我想查询和数据框并输出第一个索引中的项目所有,如果它包含在第二个索引中。 描述我想要实现的目标的简化版本是:
data = {'colour': ['red','purple','green','purple','blue','red'], 'item': ['hat','scarf','belt','belt','hat','scarf'], 'material': ['felt','wool','leather','wool','plastic','wool']}
df = pd.DataFrame(data=data)
grpd_df = df.groupby(df['item']).apply(lambda df: df.reset_index(drop=True))
grpd_df
colour item material
item
belt 0 green belt leather
1 purple belt wool
hat 0 red hat felt
1 blue hat plastic
scarf 0 purple scarf wool
1 red scarf wool
我想获取项目中具有红色项目的所有行:
hat 0 red hat felt
1 blue hat plastic
scarf 0 purple scarf wool
1 red scarf wool
答案 0 :(得分:3)
将groupby
与2系列进行比较,将color
列与eq
与any
进行比较,每组至少有一个True
:
df = grpd_df[grpd_df['colour'].eq('red').groupby(level=0).transform('any')]
print (df)
colour item material
item
hat 0 red hat felt
1 blue hat plastic
scarf 0 purple scarf wool
1 red scarf wool
<强>详细强>:
print (grpd_df['colour'].eq('red').groupby(level=0).transform('any'))
item
belt 0 False
1 False
hat 0 True
1 True
scarf 0 True
1 True
Name: colour, dtype: bool
使用filter
的更慢的替代方案:
df = grpd_df.groupby(level=0).filter(lambda x: x['colour'].eq('red').any())
如果想使用原始DataFrame
:
df = df[df['colour'].eq('red').groupby(df['item']).transform('any')]
print (df)
colour item material
0 red hat felt
1 purple scarf wool
4 blue hat plastic
5 red scarf wool
编辑:
如果想使用MultiIndex
:
data = {'colour': ['red','purple','green','purple','blue','red'], 'item': ['hat','scarf','belt','belt','hat','scarf'], 'material': ['felt','wool','leather','wool','plastic','wool']}
df = pd.DataFrame(data=data).set_index(['colour','item'])
print (df)
material
colour item
red hat felt
purple scarf wool
green belt leather
purple belt wool
blue hat plastic
red scarf wool
df = df[pd.Series(df.index.get_level_values('colour') == 'red', index=df.index).groupby(level=1).transform('any')]
第二个filter
解决方案:
df = df.groupby(level=1).filter(lambda x: (x.index.get_level_values('colour') == 'red').any())
print (df)
material
colour item
red hat felt
purple scarf wool
blue hat plastic
red scarf wool