如何通过其值重新索引数据框?

时间:2016-02-16 05:06:18

标签: python pandas

hdfs://hmaster:54310

我上面有一个数据框,如何将其转换为以下数据框?

country  doctor  teacher  lawyer
China      41       26      34
USA        25       29      40
Japan      20       26      27

2 个答案:

答案 0 :(得分:3)

df.set_index('country').stack()

(或者您可能真的希望df.set_index('country').stack().reset_index().rename(columns={'level_1': 'job', 0: 'age'})更直接地提出问题 - 细节取决于您正在做的事情。)

答案 1 :(得分:2)

您可以使用set_indexunstack

In [65]: df.set_index('country').unstack().reset_index()
Out[65]:
   level_0 country   0
0   doctor   China  41
1   doctor     USA  25
2   doctor   Japan  20
3  teacher   China  26
4  teacher     USA  29
5  teacher   Japan  26
6   lawyer   China  34
7   lawyer     USA  40
8   lawyer   Japan  27

或者,如果您想age成为结果的索引,可以添加set_index

In [66]: df.set_index('country').unstack().reset_index().set_index(0)
Out[66]:
    level_0 country
0
41   doctor   China
25   doctor     USA
20   doctor   Japan
26  teacher   China
29  teacher     USA
26  teacher   Japan
34   lawyer   China
40   lawyer     USA
27   lawyer   Japan

重命名和重新排序列:

res = df.set_index('country').unstack().reset_index()
res.columns = ['job', 'country', 'age']
res = res[['age', 'job', 'country']]

In [73]: res
Out[73]:
   age      job country
0   41   doctor   China
1   25   doctor     USA
2   20   doctor   Japan
3   26  teacher   China
4   29  teacher     USA
5   26  teacher   Japan
6   34   lawyer   China
7   40   lawyer     USA
8   27   lawyer   Japan