2013-07-19 51 views
1

我正在嘗試編寫一個腳本,用於根據這些質量列表計算一組質量的分數。我的數據框看起來是這樣的:將熊貓數據框的所有非數字行合併爲一個

  qual1   qual2 ... score 
8 intellectual intellectual ...  8 
7   funny charismatic ...  7 
6   witty  ambitious ...  6 
5  outgoing  honest ...  5 
4 adventurous  active ...  4 
3   NaN adventurous ...  3 
2   NaN  outgoing ...  2 

我希望得到的東西是這樣的:

 quals  score 
intellectual  8 
funny    7 
witty    6 
outgoing   5 
adventurous   4 
intellectual  8 
charismatic   7 
ambitious   6 
honest    5 
active    4 
adventurous   3 
outgoing   2 

我不知道如何去了解這一點,所以黯然無代碼示例:(

回答

4

一種方法是使用pd.melt

>>> df 
      qual1   qual2 score 
0 intellectual intellectual  8 
1   funny charismatic  7 
2   witty  ambitious  6 
3  outgoing  honest  5 
4 adventurous  active  4 
5   NaN adventurous  3 
6   NaN  outgoing  2 
>>> pd.melt(df, "score").rename(columns={"value": "quals"}).dropna()[["quals", "score"]] 
      quals score 
0 intellectual  8 
1   funny  7 
2   witty  6 
3  outgoing  5 
4 adventurous  4 
7 intellectual  8 
8 charismatic  7 
9  ambitious  6 
10  honest  5 
11  active  4 
12 adventurous  3 
13  outgoing  2