Python pandas通过对现有列进行分组来创建其他数据帧列

时间:2015-07-01 19:26:23

标签: python pandas filter merge group-by

尝试从现有列的内容创建新的数据框列。用例子更容易解释。我想转换一下:



.	Yr	Month	Class	Cost
1	2015	1	L	19.2361
2	2015	1	M	29.4723
3	2015	1	S	48.5980
4	2015	1	T	169.7630
5	2015	2	L	19.1506
6	2015	2	M	30.0886
7	2015	2	S	49.3765
8	2015	2	T	167.0000
9	2015	3	L	19.3465
10	2015	3	M	29.1991
11	2015	3	S	46.2580
12	2015	3	T	157.7916
13	2015	4	L	18.3165
14	2015	4	M	28.2314
15	2015	4	S	44.5844
16	2015	4	T	162.3241
17	2015	5	L	17.4556
18	2015	5	M	27.0434
19	2015	5	S	42.8841
20	2015	5	T	159.3457
21	2015	6	L	16.5343
22	2015	6	M	24.9853
23	2015	6	S	40.5612
24	2015	6	T	153.4902




...进入以下内容,以便我可以绘制4个单独的行[L,M,S,T]:



.	 Yr	  Month	   L	   M	   S	   T
1	2015	1	19.2361	29.4723	48.5980	169.7630
2	2015	2	19.1506	30.0886	49.3765	167.0000
3	2015	3	19.3465	29.1991	46.2580	157.7916
4	2015	4	18.3165	28.2314	44.5844	162.3241
5	2015	5	17.4556	27.0434	42.8841	159.3457
6	2015	6	16.5343	24.9853	40.5612	153.4902




我能够以一种非常笨拙的方式解决这个问题,通过过滤“班级”中的数据框架。列...然后3个单独的合并。

list_class = ['L', 'M', 'S', 'T']
year = 'Yr'
month = 'Month'
df_class = pd.DataFrame()
df_class1 = pd.DataFrame()
df_class2 = pd.DataFrame()
df_class1 = merge(df[[month, year, 'Class','Cost']][df['Class']==list_class[0]], df[[month, year, 'Class','Cost']][df['Class']==list_class[1]], \
            left_on=[month, year], right_on=[month, year])
df_class2 = merge(df[[month, year, 'Class','Cost']][df['Class']==list_class[2]], df[[month, year, 'Class','Cost']][df['Class']==list_class[3]], \
            left_on=[month, year], right_on=[month, year])
df_class = merge(df_class1, df_class2, left_on=[month, year], right_on=[month, year]).groupby([year, month]).mean().plot(figsize(15,8))

必须有一种更有效的方式。感觉它应该用groupby来完成,但是我无法解决它。

1 个答案:

答案 0 :(得分:5)

您可以先将df转换为多级索引类型,然后unstack级别Class将为您提供所需内容。假设df是帖子开头显示的原始数据框。

df.set_index(['Yr', 'Month', 'Class'])['Cost'].unstack('Class')

Out[29]: 
Class             L        M        S         T
Yr   Month                                     
2015 1      19.2361  29.4723  48.5980  169.7630
     2      19.1506  30.0886  49.3765  167.0000
     3      19.3465  29.1991  46.2580  157.7916
     4      18.3165  28.2314  44.5844  162.3241
     5      17.4556  27.0434  42.8841  159.3457
     6      16.5343  24.9853  40.5612  153.4902