Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫桌子的整形_Python_Pandas_Dataframe_Reshape - Fatal编程技术网

Python 熊猫桌子的整形

Python 熊猫桌子的整形,python,pandas,dataframe,reshape,Python,Pandas,Dataframe,Reshape,我想用熊猫重塑一张桌子。我有一个表格: date | country |state | population | num_cars 1 | c1 | s1 | 1 | 1 2 | c1 | s1 | 1 | 1 1 | c1 | s2 | 1 | 1 . 2 | c2 | s2 | 1 | 2 2 | c2 | s2 | 1

我想用熊猫重塑一张桌子。我有一个表格:

date | country |state | population | num_cars
1    | c1      | s1   | 1          | 1
2    | c1      | s1   | 1          | 1
1    | c1      | s2   | 1          | 1
.
2    | c2      | s2   | 1          | 2
2    | c2      | s2   | 1          | 2
我想把它变成这个形状:

date |1_population | c1_s1_population | c1_s2_population...| c2_s1_populationc1_num_cars |c2_11_num_cars...
为了说明这一点,初始数据包含了日期范围内的pop和国家、州的数字。现在,我想将每个级别(国家、国家/地区)的时间序列转换为若干列


如何做到这一点?

作为源数据示例,我使用了一个带有2个假设值的数据帧 国家,每个国家3个:

    date country state  population  num_cars
0   1990     Xxx   Aaa         100        15
1   2010     Xxx   Aaa         120        18
2   1990     Xxx   Bbb          80         9
3   2010     Xxx   Bbb          88        11
4   1990     Xxx   Ccc          75         6
5   2010     Xxx   Ccc          82         8
6   1990     Yyy   Ggg          40         5
7   2010     Yyy   Ggg          50         6
8   1990     Yyy   Hhh          30         3
9   2010     Yyy   Hhh          38         4
10  1990     Yyy   Jjj          29         3
11  2010     Yyy   Jjj          35         4
要解决您的问题,请从定义重新格式化函数开始:

def reformat(grp, col):
    pop = grp[col]
    pop.name = grp.date.iloc[0]
    return pop
从一组行(grp)中获取一列特定名称(col), 从第一行(分组键)将名称设置为日期,然后 归还它

作为初始步骤,按国家和州对df进行分组:

然后计算2个数据帧,作为重新格式化的结果(应用 上述功能适用于每个组,适用于两个感兴趣的列:

df1 = gr.apply(reformat, col='population')
df2 = gr.apply(reformat, col='num_cars')
并在索引上合并两个部分结果:

pd.merge(df1, df2, left_index=True, right_index=True,
    suffixes=('_pop', '_cars'))
结果是:

country Xxx_pop         Yyy_pop         Xxx_cars         Yyy_cars        
state       Aaa Bbb Ccc     Ggg Hhh Jjj      Aaa Bbb Ccc      Ggg Hhh Jjj
date                                                                     
1990        100  80  75      40  30  29       15   9   6        5   3   3
2010        120  88  82      50  38  35       18  11   8        6   4   4
如您所见,列上的多重索引的顶层是“国家/人口” 和“国家/汽车编号”。另一级包含州名称

要跟踪tis解决方案的工作方式,请分别执行每个步骤并进行检查
其结果。

您可以发布样本数据吗?您如何在一个级别上获取所有内容。因此,让我们保留这些列,如Xxx AAA、Xxx_Bbb……并在同一级别上添加Xxx_pop列,以便Xxx_pop(1990:3552010:290-在国家/地区再做一个groupby,然后合并?或者有一种标准的、更短的方法吗?我采用了“模块化方法”,例如,重新格式化函数只处理一列。这就是为什么以后只需要合并两个结果(对于两个源列).当然,我并不坚持我的方法是唯一可能的。我认为它相当优雅,所以我只提出了它,但也许其他人会提出不同的解决方案。
country Xxx_pop         Yyy_pop         Xxx_cars         Yyy_cars        
state       Aaa Bbb Ccc     Ggg Hhh Jjj      Aaa Bbb Ccc      Ggg Hhh Jjj
date                                                                     
1990        100  80  75      40  30  29       15   9   6        5   3   3
2010        120  88  82      50  38  35       18  11   8        6   4   4