我有一个数据集,包含国家对的进口和出口。每个国家对都有两行,对应给定的年份。数据如下所示:
import pandas as pd
d = {
"c1_id": [1,1,1,1,2,2,2,2],
"c2_id":[2,2,2,2,1,1,1,1],
"c1_name":["Austria","Austria","Austria","Austria","Denmark","Denmark","Denmark","Denmark"],
"c2_name":["Denmark","Denmark","Denmark","Denmark","Austria","Austria","Austria","Austria"],
"year": [1970,1971,1972,1973,1970,1971,1972,1973],
"export":[10,12,14,17,19,20,45,70],
"import":[17,19,49,12,45,34,23,60]
}
df = pd.DataFrame(d)
c1_id c2_id c1_name c2_name year export import
0 1 2 Austria Denmark 1970 10 17
1 1 2 Austria Denmark 1971 12 19
2 1 2 Austria Denmark 1972 14 49
3 1 2 Austria Denmark 1973 17 12
4 2 1 Denmark Austria 1970 19 45
5 2 1 Denmark Austria 1971 20 34
6 2 1 Denmark Austria 1972 45 23
7 2 1 Denmark Austria 1973 70 60
最后,我想要一个数据框,其中一行是给定年份,列是export1、export2、import1、import2:
一个二个一个一个
有没有办法在Python中做到这一点?
我尝试使用pivot但是没有成功。我也尝试unstack但是没有成功。
2条答案
按热度按时间68bkxrlz1#
看起来使用掩码,重命名列,并执行
merge
可能是一个很好的方法:输出:
7cwmlq892#
探索另一种(快速)方法: