简体   繁体   English

从唯一 ID 创建具有第一个和最后一个日期的列

[英]Create columns with first and last date from a unique ID

I have the dataset below and I would like to extract the first and last appearance date from that specific ID.我有下面的数据集,我想从该特定 ID 中提取第一次和最后一次出现日期。 The last and first appearance is what the result should be:最后和第一次出现的结果应该是:

id            date           last_apparence     first_apparence
653777        2021-02-19     2021-02-19         2021-02-19
1873547       2021-02-19     2021-02-19         2021-02-19
657443        2021-02-19     2021-02-19         2021-02-19
653777        2021-02-20     2021-02-20         2021-02-19

So, for example, the ID 653777 shows up on the 19th and 20th, in this case, the first appearance would be 19th and the last appearance would be 20th.因此,例如,ID 653777 出现在 19 日和 20 日,在这种情况下,第一次出现是 19 日,最后一次出现是 20 日。 I tried to use我试着用

I tried to use this code below but get the same value for the entire column.我尝试在下面使用此代码,但为整个列获得相同的值。

df['latest_apparence'] = df['date'][df.index[-1]]

My last approach was to have a groupby but even trying a bunch of different groups, the closest thing I got was something similar to the countif formula from excel but then I don`t get the first/last date, only how many times the id shows in the dataset我的最后一种方法是有一个 groupby,但即使尝试了一堆不同的组,我得到的最接近的东西是类似于 excel 的 countif 公式,但是我没有得到第一个/最后一个日期,只有多少次 id显示在数据集中

df.groupby(['id'])[['date']].count()

Does anybody have any idea what is the best way to get this result?有谁知道获得此结果的最佳方法是什么?

thanks谢谢

Let us try groupby with transform让我们试试groupbytransform

df['latest_apparence'] = df.groupby('id')['date'].transform('max')
df['first_apparence'] = df.groupby('id')['date'].transform('min')

Depending on how you want your data, you can do根据您想要数据的方式,您可以执行


dt = df.groupby('id')['date'].agg(['min','max']).rename(columns={'min':'first_date','max':'last_date'})

print(dt)

# or broadcasting as Beny suggested with 
# AGG is aggregation of min and max
# df.groupby('id')['date'].transform(AGG)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过组合名字、中间名和出生日期创建唯一 ID - Create Unique ID by combining First Name, Middle Name and Date of Birth 从元组列表[(ID,date),(ID,date)..]中创建一个具有唯一ID和最新日期的元组列表 - From a list of tuples [(ID, date),(ID, date)..] create a new list of tuples with unique ID and most recent date 从现有的两列python中创建唯一ID - Create unique ID from the existing two columns, python Pandas 为最后记录日期创建列 - Pandas create columns for last record date 通过数据帧中的唯一 id 获取第一行和最后一行值 - Get the first and last row values by unique id in a dataframe 如何根据每个 ID 的第一次和最后一次审查日期列对行进行分组,并使用 python 向前填充 N/A 值? - How to group rows based on first and last review date columns for each ID and forward fill N/A values using python? Pandas Dataframe 使用 Groupby 从其他两列的唯一值创建下一个未来日期的列 - Pandas Dataframe Create Column of Next Future Date from Unique values of two other columns, with Groupby 通过组合名字和姓氏数组中的值来创建唯一的名称 - Create a unique name by combining the values in the first and last names array 从2列创建日期对象 - create date object from 2 columns 从多个 ID 列创建子 ID - Create a Sub ID from a multiple ID columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM