python/pandas将行合并为一行,并决定在每列中要做什么操作
我有一个带有列['date'(dateTime),“工作时间”(TimeDelta),'pay'(float),“付费每小时付费”(float),“距离”(距离),“距离”(float),'(float),'的pandas dataframe(data)最低薪水'(float)]
如何根据所有人相同的列值将行组合为1行,但分配了如何计算其他列的值? 我想根据同一日期['date']。dt.date
组合行。 并为每列分配不同的操作。
例如,对于列“工作时间”(sum),“付费”(sum),“每小时付费”(平均),“距离”(sum),“最低薪水”(最低)]
我找到了重复的行基于日期: data [data ['date']。dt.date.duplicated()]
但是我不确定如何结合行。另外,我使用groupby()检查了一些方法,但是我不确定在这种情况下如何使用它。
感谢您的帮助!
I have a Pandas DataFrame (data) with columns ['Date' (datetime), 'Hours Worked' (timedelta), 'Pay' (float), 'Pay per Hour' (float), 'Distance' (float), 'Minimum Salary' (float)]
How can I combine rows into 1 row, based on a column value that is the same for all, but assign how to calculate values of the other columns?
I want to combine rows based on same date ['Date'].dt.date
.
And assign different operations for each column.
For example, for columns 'Hours Worked' (sum), 'Pay' (sum), 'Pay per Hour' (average), 'Distance' (sum), 'Minimum Salary' (the lowest)]
I found the duplicated rows based on date:data[data['Date'].dt.date.duplicated()]
but I am not sure how to combine the rows. Also, I checked some methods with groupby(), but I am not sure how can I use it in this case.
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我了解问题,以下问题可能会对您有所帮助。
您确实可以使用 groupby 在您感兴趣的列上,但缺少一些东西。
首先:使用模拟数据很好。这是我的代码
请注意,每个日期都是重复的。
第二:如您所说,使用
希望这是您所需要的。
If I understood the problem, the following may help you.
You indeed can use groupby to unify the rows based on the column that you have an interest in, but there is something missing.
First: It is good to have simulated data. Here is my code for that
Note that each date is duplicated.
Second: As you said, use the groupby function, but in combination with the aggregation function, to the aggregation function you pass a dictionary with the keys as the columns of the DataFrame and the content as the function that you need, it should be something like this.
Hope it is what you need.