1
A
回答
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
相關問題
- 1. 熊貓:調換數據幀
- 2. 用整數索引熊貓數據幀
- 3. 如何使用熊貓來改變數據幀的值?
- 4. 如何使用熊貓據幀
- 5. 移調大熊貓據幀
- 6. Python的大熊貓 - JSON來數據幀
- 7. 如何使用熊貓數據幀在Seaborn數據參數lmplot
- 8. 熊貓數據幀:在數據幀
- 9. 熊貓數據幀的循環調度
- 10. 移調一個熊貓數據幀
- 11. 如何使用熊貓來阻止10幀長度的平均數據幀?
- 12. 使用大熊貓多級數據幀
- 13. 使用大熊貓數據幀
- 14. 如何旋轉大熊貓數據幀
- 15. 如何剪切熊貓數據幀
- 16. 如何在熊貓分組數據幀
- 17. 熊貓:如何在數據幀
- 18. 如何在大熊貓數據幀列
- 19. 如何從大熊貓數據幀
- 20. 如何在大熊貓數據幀
- 21. 如何從列大熊貓數據幀
- 22. 如何在大熊貓數據幀
- 23. 如何從CSV從熊貓數據幀
- 24. 來自另一個數據幀的大小熊貓數據幀
- 25. 檢查熊貓數據幀
- 26. 熊貓分層數據幀
- 27. 在熊貓數據幀蟒
- 28. Forex_python在熊貓數據幀
- 29. 熊貓樞軸數據幀
- 30. 在熊貓數據幀
小編建議 - [不要發佈的代碼圖像(或鏈接它們)](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-as king-a-question) – jezrael