简体   繁体   English

在python中groupby时选择特定行的值

[英]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.

相关问题 使用 groupby 从特定行中减去值 - Subtract value from particular row using groupby 从python中的groupby对象中选择特定行 - selecting a particular row from groupby object in python python pandas - 在groupby之后选择特定值 - python pandas - select particular values after groupby 使用 groupby 变换从特定行中减去值 - Subtract value from particular row using groupby transform Python Pandas groupby,行值到列标题 - Python Pandas groupby, row value to column headers PYTHON - PANDAS - Groupby更新行值 - PYTHON - PANDAS - Groupby update row value 如何为特定列的每个不同值选择一行并合并以在 Python 中形成新的数据框? - How to select one row for each distinct value for a particular column and merge to form a new dataframe in Python? 在 Python 中的特定行值之后,使用来自 dataframe2 和 select 中的所有行的值过滤 dataframe1 - Filter dataframe1 with values from dataframe2 and select all rows in dataframe 1 after a particular row value in Python 当同一行中的另一列为NaN时,如何从熊猫数据框中选择特定的列值? - How to select a particular column value from a pandas dataframe when another column in the same row is NaN? pandas groupby,然后按列的值选择一行(例如,最小值、最大值) - pandas groupby and then select a row by value of column (min,max, for example)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM