简体   繁体   English

如何查找和添加ID的频次列?

[英]How to find and add frequency column for ID?

I am a beginner at python, so bear with me! 我是python的初学者,请多多包涵!

My dataset is from excel and I was curious how to find and add a frequency column for my ID. 我的数据集来自excel,我很好奇如何为我的ID查找并添加频率列。

I first performed the groupby function for ID and date by doing: 我首先通过执行以下操作来对ID和日期执行groupby函数:

dfcount = dfxyz.groupby(["ID", "Date"])

and then found the mean by doing: 然后通过执行以下操作找到均值:

dfcount1 = dfcount.mean()

The output i got was: 我得到的输出是:

图片

What I am trying to do is get the frequency number beside it like this: 我正在尝试做的是获得像这样的频率编号:

图片2

I did not know how to copy python code, so I uploaded pictures! 我不知道如何复制python代码,所以我上传了图片! Sorry! 抱歉! Any help is appreciated for what code I can use to count the frequency for each ID AFTER I find the mean of the groupby columns. 在找到groupby列的平均值后,我可以使用哪些代码来计数每个ID的频率,我们将为您提供帮助。

Thank you in advance! 先感谢您!

您可以将cumcountgroupby一起cumcount

df['Freq']=(df.groupby(level=0).cumcount()+1).values

You can use this: 您可以使用此:

df['column_name'].value_counts()

value_counts - Returns object containing counts of unique values value_counts-返回包含唯一值计数的对象

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM