2017-02-24 43 views
1

我想使用Python大熊貓到數據幀轉換下面 enter image description here如何使用熊貓來調整數據幀

下一個數據幀以下

enter image description here

請原諒我,但我非常非常新的熊貓。該表已經被導入到一個名爲filterframe的數據框中。

+1

小編建議 - [不要發佈的代碼圖像(或鏈接它們)](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-as king-a-question) – jezrael

回答

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 
+0

Jezrael,你是一個高手!我無法相信你解決這個問題的速度......我在這裏尖叫。我需要多久才能習慣熊貓圖書館。本週我纔剛開始學習熊貓! – Conrad

+0

Supeer,我認爲熊貓是非常不錯的圖書館和完美的文檔;) – jezrael

+0

你有一個論壇,你可以教或聯繫的形式? – Conrad