MaxB
MaxB

Reputation: 458

Appending to elements in dictionary of dataframes

I have a dictionary of dataframes. For each element of the dictionary, I want to append an outside dataframe to the beginning.

for x in dict_of_df:
    x = df1.append(x)

In this example, df1 is a dataframe that never changes, and I want to append it to the beginning of every dataframe in my dict of dataframes. However when I do this it doesn't change any elements in the dict, and then returns a random dataframe named x with df1 appended to the beginning. Why won't this "stick" in the elements of the dict?

Upvotes: 0

Views: 101

Answers (2)

ak_slick
ak_slick

Reputation: 1016

The way you are iterating through the dictionary you will only get the keys. give this a try to get to the dataframe values.

I find using .items() is a clean way to handle this.

for key, df in dict_of_df.items():
    dict_of_df[key] = df1.append(df)

Upvotes: 1

Andy L.
Andy L.

Reputation: 25239

x in every loop of your for-loop is key of the dict. Try this

for x in dict_of_df:
    dict_of_df[x] = df1.append(dict_of_df[x])

Upvotes: 1

Related Questions