Home > Software engineering >  How can I exchange rows for multiple dataframes?
How can I exchange rows for multiple dataframes?

Time:03-17

I have a dictionary where each value contains a number of different DataFrame that is grouped by Name, but I'd like to make it so that instead they are grouped by Time. I'm trying to apply a group-by/apply/split type function, but because they are not a single DataFrame (but rather split DataFrames in a dictionary), I'm a bit stuck.

Toy example of a dictionary with 2 dataframes:

{'df1':    Name  Time
 0  Greg  1-10
 1  Greg  5-10
 'df2':     Name  Time
 0   Jeff  5-10
 1  Jeff  1-10}

To reproduce:

d = {'df1': pd.DataFrame({'Name': ["Jeff", "Greg"], 'Times': ["1-5", "5-10"]}), 'df2': pd.DataFrame({'Name': ["Jeff", "Greg"], 'Times': ["5-10", "1-5"]})}

Desired output:

{'df1':    Name  Time
 0  Greg  1-10
 1  Jeff  1-10
 'df2':     Name  Time
 0  Jeff  5-10
 1  Greg  5-10}

CodePudding user response:

IIUC, your data looks like below (let's call it data):

{'df1':    Name.  Time
 0  Greg.  1-10
 1  Greg.  5-10,
 'df2':     Name  Time
 0   Jeff  5-10
 1  Jeff.  1-10}

Here's one option: concat groupby "Time" dict comprehension:

out = {f'df{i}': df for i, (_, df) in 
       enumerate(pd.concat((data['df1'].rename(columns={'Name.':'Name'}), 
                            data['df2'])).groupby('Time'), 1)}

Output:

{'df1':     Name  Time
 0  Greg.  1-10
 1  Jeff.  1-10,
 'df2':     Name  Time
 1  Greg.  5-10
 0   Jeff  5-10}
  • Related