Appending DataFrame to empty DataFrame in {Key: Empty DataFrame (with columns)}

Viewed 272

I am struggling to understand this one.

I have a regular df (same columns as the empty df in dict) and an empty df which is a value in a dictionary (the keys in the dict are variable based on certain inputs, so can be just one key/value pair or multiple key/value pairs - think this might be relevant). The dict structure is essentially:

{key: [[Empty DataFrame
Columns: [list of columns]
Index: []]]}

I am using the following code to try and add the data:

dict[key].append(df, ignore_index=True)

The error I get is:

temp_dict[product_match].append(regular_df, ignore_index=True)
TypeError: append() takes no keyword arguments

Is this error due to me mis-specifying the value I am attempting to append the df to (like am I trying to append the df to the key instead here) or something else?

2 Answers

Your dictionary contains a list of lists at the key, we can see this in the shown output:

{key: [[Empty DataFrame Columns: [list of columns] Index: []]]}
#     ^^ list starts                                        ^^ list ends

For this reason dict[key].append is calling list.append as mentioned by @nandoquintana.

To append to the DataFrame access the specific element in the list:

temp_dict[product_match][0][0].append(df, ignore_index=True)

Notice there is no inplace version of append. append always produces a new DataFrame:

Sample Program:

import numpy as np
import pandas as pd

temp_dict = {
    'key': [[pd.DataFrame()]]
}

product_match = 'key'

np.random.seed(5)
df = pd.DataFrame(np.random.randint(0, 100, (5, 4)))

temp_dict[product_match][0][0].append(df, ignore_index=True)
print(temp_dict)

Output (temp_dict was not updated):

{'key': [[Empty DataFrame
Columns: []
Index: []]]}

The new DataFrame will need to be assigned to the correct location.

Either a new variable:

some_new_variable = temp_dict[product_match][0][0].append(df, ignore_index=True)

some_new_variable

    0   1   2   3
0  99  78  61  16
1  73   8  62  27
2  30  80   7  76
3  15  53  80  27
4  44  77  75  65

Or back to the list:

temp_dict[product_match][0][0] = (
    temp_dict[product_match][0][0].append(df, ignore_index=True)
)

temp_dict

{'key': [[    0   1   2   3
0  99  78  61  16
1  73   8  62  27
2  30  80   7  76
3  15  53  80  27
4  44  77  75  65]]}

Assuming there the DataFrame is actually an empty DataFrame, append is unnecessary as simply updating the value at the key to be that DataFrame works:

temp_dict[product_match] = df

temp_dict

{'key':     0   1   2   3
0  99  78  61  16
1  73   8  62  27
2  30  80   7  76
3  15  53  80  27
4  44  77  75  65}

Or if list of list is needed:

temp_dict[product_match] = [[df]]

temp_dict

{'key': [[    0   1   2   3
0  99  78  61  16
1  73   8  62  27
2  30  80   7  76
3  15  53  80  27
4  44  77  75  65]]}
Related