Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas/ Python list values of one column based on string value of another column

Tags:

python

pandas

I have a .csv file like this:

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

I want to get the Receipt IDs where the Category Type is "Food".

I've tried a few methods but none of them work. For example,

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

I've also tried setting Category Type as index:

df1 = df.set_index('Category Type').eq('Food')
print (df1.index[df1['Receipt ID']].tolist())

which gave me an empty list.

The Receipt IDs are not necessarily unique, although I want the outputs to be unique, and the final goal is to find the Receipt ID that contains both "Food" and "Drink". Could any expert please give me some help? Thank you!

like image 456
qyy Avatar asked Dec 14 '25 22:12

qyy


1 Answers

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

if you want unique:

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

or

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

for all types:

df.groupby('Category Type').agg({'Receipt ID': 'unique'}).to_dict()
like image 155
MoRe Avatar answered Dec 16 '25 13:12

MoRe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!