Skip to content Skip to sidebar Skip to footer

How To Split A Data Frame Into Multiple Data Frames Based On Columns

I have the following data frames: import pandas as pd df = pd.DataFrame( { 'Name' : ['gene1','gene2','gene3','gene4'] , 'T1' : [0.33,1,3,4], 'T2' : [1.23,2.1,3.5,5.0]

Solution 1:

You can get there using pd.melt():

melted = pd.melt(df, id_vars='Name', var_name='t_col')

for t_col, sub_df in melted.groupby('t_col'):
    print(sub_df)

    Name t_col  value
0  gene1    T1   0.33
1  gene2    T1   1.00
2  gene3    T1   3.00
3  gene4    T1   4.00
    Name t_col  value
4  gene1    T2   1.23
5  gene2    T2   2.10
6  gene3    T2   3.50
7  gene4    T2   5.00

Post a Comment for "How To Split A Data Frame Into Multiple Data Frames Based On Columns"