[英]Is there anyway to ungroup data in a grouped-by pandas dataframe?
I have a dataset that for simplicity I need to group by and aggregate based on one column so that I can remove some rows easily. 我有一个数据集,为简单起见,我需要根据一个列进行分组和汇总,以便可以轻松删除一些行。 Once I am done with the calculations, I need to reverse the group by actions so that I can see the dataframe easily in excel.
一旦完成计算,我就需要按操作反转分组,以便可以在excel中轻松查看数据框。 If I do not inverse the action, I would export the whole list to excel which is not easy to analyse.
如果我不采取相反的行动,我会将整个列表导出到excel,这很难分析。 Any help is gretaly appreciated.
任何帮助将深表感谢。
Example: 例:
Col1 Col2 Col3
123 11 Yes
123 22 Yes
256 33 Yes
256 33 No
337 00 No
337 44 No
After applying groupby and aggregate: 应用groupby和聚合后:
X=dataset.groupby('Col1').agg(lambda x:set(x)).reset_index()
I get 我懂了
Col1 Col2 Col3
123 {11,22} {Yes}
256 {33} {Yes, No}
337 {00,44} {No}
I then remove all the columns that contain Yes using drop 然后,我使用drop删除所有包含Yes的列。
X=X.reset_index(drop=True)
what I need to get before exporting to excel is 在导出到excel之前我需要获得的是
Col1 Col2 Col3
337 00 No
337 44 No
Hope this is clear enough 希望这足够清楚
Thaks in advance 提前解冻
I don't believe converting to a set is a good idea. 我认为转换成集合不是一个好主意。 Here's an alternative: First sort in descending order by
Col3
, then create a mapping of Col2 : Yes/No
and filter based on that. 这是一种替代方法:首先按
Col3
降序排序,然后创建Col2 : Yes/No
的映射Col2 : Yes/No
并基于此进行过滤。
In [1191]: df = df.sort_values('Col3', ascending=True)
In [1192]: mapping = dict(df[['Col2', 'Col3']].values)
In [1193]: df[df.Col2.replace(mapping) == 'No'] # or df.Col2.map(mapping)
Out[1193]:
Col1 Col2 Col3
4 337 0 No
5 337 44 No
I am agree with COLDSPEED. 我同意COLDSPEED。 You do not need convert to set
您不需要转换为设置
df['Temp']=df.Col3.eq('Yes')
DF=df.groupby('Col1')['Temp'].sum()
df[df.Col1==DF.index[DF==0].values[0]].drop('Temp',axis=1)
Out[113]:
Col1 Col2 Col3
4 337 0 No
5 337 44 No
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.