Pandas/ Python 根据另一列的字符串值列出一列的值答案

作者: 分类: 编程代码 时间:1970-01-01

Pandas/ Python 根据另一列的字符串值列出一列的值答案

Pandas/ Python list values of one column based on string value of another columnPandas/ Python 根据另一列的字符串值列出一列的值

我有一个像这样的 .csv,包含这些列:

Receipt ID Name Quantity Category Type
135135 Croissant 1.0 Food
135135 Cappucino 1.0 Drink
143143 Salad 1.0 Food
154134 Americano 1.0 Drink
178781 Cappucino 1.0 Drink
169071 Muffin 1.0 Food
169071 Latte 1.0 Drink
169071 Brownie 1.0 Food

我想获取“类别类型”为“食物”的“收据 ID”

我尝试了一些方法,但没有一个有效

df1 = df.query('Category Type == Food')['Receipt ID'].unique()

不工作

我也尝试将类别类型设置为索引

df1 = df.set_index('Category Type').eq('Food')

print (df1.index[df1['Receipt ID']].tolist())

这给了我一个空列表

收据 ID 不一定是唯一的,尽管我希望输出是唯一的,最终目标是找到包含食物和饮料的收据 ID。请哪位专家给我一些帮助好吗?谢谢!

【问题讨论】:

标签: python-3.x pandas


【解决方案1】:
df.where(df['Category Type'] == 'Food')['Receipt ID'].dropna().values.tolist()

如果你想要独一无二:

df.where(df['Category Type'] == 'Food')['Receipt ID'].dropna().unique().astype(int).tolist()

df.loc[df['Category Type'] == 'Food', 'Receipt ID'].unique().tolist()

适用于所有类型:

df.groupby('Category Type').agg({'Receipt ID': 'unique'}).to_dict()

【讨论】:

    【解决方案2】:
    
    import pandas as pd
    from io import StringIO
    
    data_str = """
    Receipt ID  Name    Quantity    Category Type
    135135  Croissant   1.0 Food
    135135  Cappucino   1.0 Drink
    143143  Salad   1.0 Food
    154134  Americano   1.0 Drink
    178781  Cappucino   1.0 Drink
    169071  Muffin  1.0 Food
    169071  Latte   1.0 Drink
    169071  Brownie 1.0 Food
    """
    # This is myself organizing the data, you can skip it here
    io_str = StringIO(data_str)
    df = pd.read_csv(io_str, header=0, sep='\t')
    
    # start here
    filter_df = df[df['Category Type'] == 'Food']
    unique_list = filter_df['Receipt ID'].unique().tolist()
    print(unique_list)
    
    
    """
    [135135, 143143, 169071]
    """
    
    
    

    【讨论】: