python - Pandas 多索引: print all of first index if in second index

标签 python pandas

我想查询数据帧并输出第一个索引中的所有项目(如果它包含在第二个索引中)。 描述我想要实现的目标的简化版本是:

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 

最佳答案

使用groupby通过将 color 列与 eq 与 2 系列进行比较与 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')]

第二个过滤器解决方案:

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

关于python - Pandas 多索引: print all of first index if in second index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50021375/

相关文章:

python - 如何运行以逗号和空格分隔的列表作为参数的 Python 脚本?

python - 从 keras 模型中提取特征到数据集中

Python 通过 POST 发送一个 JSON 数组

python - pandas 重新采样时间序列数据 - 同一列上有多个聚合函数?

python - 即使连接关闭,Pandas read_sql_query 仍在后台运行?

python - python中不同维度数组的二维插值

python - 如何将字符串中的数字引用转换为 Python 中的整数?

python - 在 Pandas 图中标记内插的 NaN 点

python - 根据总 str 长度选择字符串的中间部分到新列 pandas

python - 将元组列表的拆分列从“应用到新列”