答案 0 :(得分:4)
使用melt
:
df1 = pd.melt(df, id_vars=['date','County'], var_name='situtation', value_name='score')
print (df1)
date County situtation score
0 22/01/2000 Buckingham Outstanding 20.0
1 22/01/2000 London Outstanding 46.0
2 24/06/2000 Manchester Outstanding 13.0
3 02/07/2000 Ipswitch Outstanding 66.0
4 04/0/2000 Crawley Outstanding 34.0
5 22/01/2000 Buckingham Okay 34.0
6 22/01/2000 London Okay 35.0
7 24/06/2000 Manchester Okay 6.0
8 02/07/2000 Ipswitch Okay 5.0
9 04/0/2000 Crawley Okay 88.0
10 22/01/2000 Buckingham Good 9.0
11 22/01/2000 London Good 3.0
12 24/06/2000 Manchester Good 2.0
13 02/07/2000 Ipswitch Good 66.0
14 04/0/2000 Crawley Good 56.0
15 22/01/2000 Buckingham Inferior 6.0
16 22/01/2000 London Inferior NaN
17 24/06/2000 Manchester Inferior NaN
18 02/07/2000 Ipswitch Inferior NaN
19 04/0/2000 Crawley Inferior NaN
20 22/01/2000 Buckingham Very Inferior 65.0
21 22/01/2000 London Very Inferior 7.0
22 24/06/2000 Manchester Very Inferior 7.0
23 02/07/2000 Ipswitch Very Inferior 56.0
24 04/0/2000 Crawley Very Inferior 57.0
stack
的另一种解决方案 - 默认为dropna=True
:
df1 = df.set_index(['date','County']).stack().reset_index()
df1.columns = ['date','County','situtation','score']
print (df1)
date County situtation score
0 22/01/2000 Buckingham Outstanding 20.0
1 22/01/2000 Buckingham Okay 34.0
2 22/01/2000 Buckingham Good 9.0
3 22/01/2000 Buckingham Inferior 6.0
4 22/01/2000 Buckingham Very Inferior 65.0
5 22/01/2000 London Outstanding 46.0
6 22/01/2000 London Okay 35.0
7 22/01/2000 London Good 3.0
8 22/01/2000 London Very Inferior 7.0
9 24/06/2000 Manchester Outstanding 13.0
10 24/06/2000 Manchester Okay 6.0
11 24/06/2000 Manchester Good 2.0
12 24/06/2000 Manchester Very Inferior 7.0
13 02/07/2000 Ipswitch Outstanding 66.0
14 02/07/2000 Ipswitch Okay 5.0
15 02/07/2000 Ipswitch Good 66.0
16 02/07/2000 Ipswitch Very Inferior 56.0
17 04/0/2000 Crawley Outstanding 34.0
18 04/0/2000 Crawley Okay 88.0
19 04/0/2000 Crawley Good 56.0
20 04/0/2000 Crawley Very Inferior 57.0
df1 = df.set_index(['date','County']).stack(dropna=False).reset_index()
df1.columns = ['date','County','situtation','score']
print (df1)
date County situtation score
0 22/01/2000 Buckingham Outstanding 20.0
1 22/01/2000 Buckingham Okay 34.0
2 22/01/2000 Buckingham Good 9.0
3 22/01/2000 Buckingham Inferior 6.0
4 22/01/2000 Buckingham Very Inferior 65.0
5 22/01/2000 London Outstanding 46.0
6 22/01/2000 London Okay 35.0
7 22/01/2000 London Good 3.0
8 22/01/2000 London Inferior NaN
9 22/01/2000 London Very Inferior 7.0
10 24/06/2000 Manchester Outstanding 13.0
11 24/06/2000 Manchester Okay 6.0
12 24/06/2000 Manchester Good 2.0
13 24/06/2000 Manchester Inferior NaN
14 24/06/2000 Manchester Very Inferior 7.0
15 02/07/2000 Ipswitch Outstanding 66.0
16 02/07/2000 Ipswitch Okay 5.0
17 02/07/2000 Ipswitch Good 66.0
18 02/07/2000 Ipswitch Inferior NaN
19 02/07/2000 Ipswitch Very Inferior 56.0
20 04/0/2000 Crawley Outstanding 34.0
21 04/0/2000 Crawley Okay 88.0
22 04/0/2000 Crawley Good 56.0
23 04/0/2000 Crawley Inferior NaN
24 04/0/2000 Crawley Very Inferior 57.0