2024-04-20 10:09:33 发布
网友
我有groupby对象
grouped = df.groupby('name') for k,group in grouped: print group
有3组bar,foo和foobar
我需要过滤这些组,并删除所有时间不超过5的组。 在我的示例中,应该删除组foo。 我正在尝试使用函数filter()
grouped.filter(lambda x: (x.max()['time']>5))
但显然,x不仅仅是数据帧格式的组。在
假设最后一行代码应该是>5,而不是>20,那么您可以做类似的事情:
>5
>20
grouped.filter(lambda x: (x.time > 5).any())
正如您正确地发现的那样,x实际上是一个DataFrame,其中name列与for循环中k中的键相匹配。在
x
DataFrame
name
k
因此,您需要根据time列中是否有大于5的倍数进行筛选,您可以执行上面的(x.time > 5).any()来测试它。在
(x.time > 5).any()
我还不习惯Python,纽比和熊猫。但我正在研究一个类似问题的解决方案,所以让我以这个问题为例来报告我的答案。在
import pandas as pd df = pd.DataFrame() df['name'] = ['foo', 'foo', 'bar', 'bar', 'foobar', 'foobar'] df['time'] = [5, 2, 5, 6, 20, 1] grouped = df.groupby('name') for k, group in grouped: print(group)
filter_time_max = grouped['time'].max() > 5 groups_should_keep = filter_time_max.loc[filter_time_max].index result2 = df.loc[df['name'].isin(groups_should_keep)]
filter_time_max = grouped['time'].max() <= 5 groups_should_drop = filter_time_max.loc[filter_time_max].index result3 = df.drop(df[df['name'].isin(groups_should_drop)].index)
name time 2 bar 5 3 bar 6 4 foobar 20 5 foobar 1
我的答案1不使用组名来删除组。如果需要组名,可以通过写:df.loc[indexes_should_drop].name.unique()来获得。在
df.loc[indexes_should_drop].name.unique()
grouped['time'].max() <= 5和grouped.apply(lambda x: (x['time'].max() <= 5)).index返回相同的结果。在
grouped['time'].max() <= 5
grouped.apply(lambda x: (x['time'].max() <= 5)).index
filter_time_max的索引是一个组名。它不能用作索引或标签,不能按原样删除。在
filter_time_max
name foo True bar False foobar False Name: time, dtype: bool
假设最后一行代码应该是
>5
,而不是>20
,那么您可以做类似的事情:正如您正确地发现的那样,
x
实际上是一个DataFrame
,其中name
列与for循环中k
中的键相匹配。在因此,您需要根据time列中是否有大于5的倍数进行筛选,您可以执行上面的
(x.time > 5).any()
来测试它。在我还不习惯Python,纽比和熊猫。但我正在研究一个类似问题的解决方案,所以让我以这个问题为例来报告我的答案。在
我的回答1:
^{pr2}$我的回答2:
我的回答3:
结果
点
我的答案1不使用组名来删除组。如果需要组名,可以通过写:
df.loc[indexes_should_drop].name.unique()
来获得。在grouped['time'].max() <= 5
和grouped.apply(lambda x: (x['time'].max() <= 5)).index
返回相同的结果。在filter_time_max
的索引是一个组名。它不能用作索引或标签,不能按原样删除。在相关问题 更多 >
编程相关推荐