[英]Select the value of particular row when groupby in python
I want to groupby multiple column based on a particular column in python.我想根据python中的特定列对多个列进行分组。 When doing so, for a particular column I want the value to last but one row'th value.这样做时,对于特定的列,我希望该值能够持续到第一个行的值。 If I wanted the same for all column, I would've have used nth method.如果我希望所有列都相同,我会使用第 n 种方法。 but I want it for a particular column and I couldn't find the solution online.但我想要一个特定的专栏,我在网上找不到解决方案。 How can I do this?我怎样才能做到这一点?
Aggregated_Halt = df.groupby('Group_Halt').agg({'Date_Time': [**last but one row**], 'Latitude': ['last'],'Longitude': ['last'], 'Time_diff':'sum', 'LocationAddress': 'last'})
Here I want datetime column to have last but one row value.在这里,我希望日期时间列具有最后一行值。
尝试这个:
Aggregated_Halt = df.groupby('Group_Halt').agg({'Date_Time': lambda x: x[-2], 'Latitude': ['last'],'Longitude': ['last'], 'Time_diff':'sum', 'LocationAddress': 'last'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.