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

1 回答 1

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
于 2013-07-19T02:46:56.790 回答