arnold
arnold

Reputation: 618

Pandas make a new dataframe with the old column names

I need some helps structure a data. So I have the following DataFrame (called df): the original dataframe

I want to group my dataframe based on Mean_CArea, Mean_CPressure, and Mean_Force. However, I got the following result:

wrongresult

As you may see the column names are 0,1,2 not NATIVE_RH, ANATOMICAL_RH, and NON_ANATOMICAL_RH. Is there a way to get the right column names from the original dataframe?

Here is my code so far:

def function(self, df):
    d = dict()
    for head in df.columns.tolist():
        RH, j_mechanics = head
        if j_mechanics not in d:
            d[j_mechanics] = df[head]
        else:
            d[j_mechanics] = pd.concat([d[j_mechanics],df[head]], axis=1, ignore_index=True)
    for df_name, df in sorted(d.items()):
        print(df_name)
        print(df.head())

Big thanks in advance!

Upvotes: 2

Views: 376

Answers (2)

piRSquared
piRSquared

Reputation: 294546

You want to use xs

df.xs('Mean_CArea', axis=1, level=1)

and

df.xs('Mean_CPressure', axis=1, level=1)

and

df.xs('Mean_Force', axis=1, level=1)

Upvotes: 1

jezrael
jezrael

Reputation: 863741

IIUC you can use swaplevel with groupby by columns (axis=1) and by first level (level=0):

df = pd.DataFrame({('B', 'a'): {0: 4, 1: 10}, ('B', 'b'): {0: 5, 1: 11}, ('B', 'c'): {0: 6, 1: 12}, ('A', 'a'): {0: 1, 1: 7}, ('A', 'c'): {0: 3, 1: 9}, ('A', 'b'): {0: 2, 1: 8}})

print (df)
   A         B        
   a  b  c   a   b   c
0  1  2  3   4   5   6
1  7  8  9  10  11  12
df.columns = df.columns.swaplevel(0,1)

for i, g in df.groupby(level=0, axis=1):
    print (g)
   a    
   A   B
0  1   4
1  7  10
   b    
   A   B
0  2   5
1  8  11
   c    
   A   B
0  3   6
1  9  12

Upvotes: 2

Related Questions