Convert columns into multiple rows in pandas dataframe
You can use melt
method.
df = pd.melt(d, id_vars=["Deal", "Year", "Financial_Data"],
value_name="Quarter").drop(['variable'],axis=1).sort_values('Quarter')
Output
Deal Year Financial_Data Quarter
0 1 1991 120 1
3 1 1991 120 2
6 1 1991 120 3
1 2 1992 80 4
4 2 1992 80 5
7 2 1992 80 6
2 3 1993 100 7
5 3 1993 100 8
8 3 1993 100 9
If you have many columns, you can use df.columns.tolist()
method in order to achieve your requirement.
column_list = df.columns.tolist()
id_vars_list = column_list[:2] + column_list[-1:]
The statement will become
df = pd.melt(d, id_vars=id_vars_list,
value_name="Quarter").drop(['variable'],axis=1).sort_values('Quarter')
This is done using melt
:
pd.melt(df, id_vars=['Deal','Year','Financial_Data'], value_vars=['Quarter_1','Quarter_2','Quarter_3'])
Deal Year Financial_Data variable value
0 1 1991 120 Quarter_1 1
1 2 1992 80 Quarter_1 4
2 3 1993 100 Quarter_1 7
3 1 1991 120 Quarter_2 2
4 2 1992 80 Quarter_2 5
5 3 1993 100 Quarter_2 8
6 1 1991 120 Quarter_3 3
7 2 1992 80 Quarter_3 6
8 3 1993 100 Quarter_3 9
Cleaning it up a little:
>>> pd.melt(df, id_vars=['Deal','Year','Financial_Data'], value_vars=['Quarter_1','Quarter_2','Quarter_3']).drop('variable',axis=1).sort_values('value')
Deal Year Financial_Data value
0 1 1991 120 1
3 1 1991 120 2
6 1 1991 120 3
1 2 1992 80 4
4 2 1992 80 5
7 2 1992 80 6
2 3 1993 100 7
5 3 1993 100 8
8 3 1993 100 9