0

Given a dictionary with multiple dataframes in it. How I can add a column to each dataframe with all the rows in that df filled with the key name'?

Dictionary Structure

I tried this code:

for key, df in sheet_to_df_map.items():
    df['sheet_name'] = key

This code does add the key column in each dataframe inside the dictionary, but also creates an additional dataframe.

DF

Can't this be done without creating an additional dataframe?

Furthermore, I want to separate dataframes from the dictionary by number of columns. All the dataframes that have 10 columns concatenated, the ones with 9 concatenated and so on. I don't know how to do this.

Steven González
  • 227
  • 1
  • 12

1 Answers1

0

I could do it with the method assign() in the DataFrames and then replacing the hole value in the dictionary, but I don't know in fact if it's this that you want...

 for key, df in myDictDf.items():    
   myDictDf[key] = df.assign(sheet_name=[key for w in range(len(df.index))])

To sort your dictionary, I think you can use an OrderedDict with the columns property of the DataFrames. By using len(df.columns) you can get the quantity of columns for each frame.

I think these links can be useful for you:

https://note.nkmk.me/en/python-pandas-len-shape-size/

https://www.geeksforgeeks.org/python-sort-python-dictionaries-by-key-or-value/

I've found a related question too: Adding new column to existing DataFrame in Python pandas