gpt4 book ai didi

python - 根据记录的大小/数量删除 Pandas 记录的优雅方式

转载 作者:太空宇宙 更新时间:2023-11-04 11:10:44 24 4
gpt4 key购买 nike

这不是重复的。我没有尝试根据索引删除行

我有一个如下所示的数据框

df = pd.DataFrame({
'subject_id':[1,1,1,1,1,1,1,2,2,2,2,2],
'time_1' :['2173-04-03 12:35:00','2173-04-03 12:50:00','2173-04-05
12:59:00','2173-05-04 13:14:00','2173-05-05 13:37:00','2173-07-06
13:39:00','2173-07-08 11:30:00','2173-04-08 16:00:00','2173-04-09
22:00:00','2173-04-11 04:00:00','2173- 04-13 04:30:00','2173-04-14
08:00:00'],
'val' :[5,2,3,1,1,6,5,5,8,3,4,6]})
df['time_1'] = pd.to_datetime(df['time_1'])
df['day'] = df['time_1'].dt.day

enter image description here

如果它们的计数是 <=5,我想删除基于 subject_id 的记录。

这是我尝试过的

df1 = df.groupby(['subject_id']).size().reset_index(name='counter')
df1[df1['counter']>5] # this gives the valid subject_id = 1 has count more than 5)

现在使用此 subject_id,我必须获取该 subject_id 的基本数据帧行

可能有一种优雅的方式来做到这一点。

我想得到如下所示的输出。我想要我的基本数据框行

enter image description here

最佳答案

使用:

df[df.groupby('subject_id')['subject_id'].transform('size')>5]

输出:

   subject_id              time_1  val  day
0 1 2173-04-03 12:35:00 5 3
1 1 2173-04-03 12:50:00 2 3
2 1 2173-04-05 12:59:00 3 5
3 1 2173-05-04 13:14:00 1 4
4 1 2173-05-05 13:37:00 1 5
5 1 2173-07-06 13:39:00 6 6
6 1 2173-07-08 11:30:00 5 8

关于python - 根据记录的大小/数量删除 Pandas 记录的优雅方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58283975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com