-1

I actually want the dishes a particular user ordered from the given dataset. For example, if I give user_id = U1077, I want to get the dishes he ordered in return. I am using python code. Can anyone help me with this.

user_id dish
U1077 Prawns Biryani
U1077 Chilli Potatoes
U1077 Paneer Multani Tikka
U1068 Kadaai Paneer
U1068 Veg biryani
U1068 Mushroom Malai Tikka
U1067 Chilli Mushroom
U1077 Lacha Paratha

I can't figure out how to do it

  • What have you tried so far? Have you tried googling "getting subset of pandas dataframe" or "filtereing pandas dataframe"? – CutePoison Nov 07 '22 at 05:14
  • Does this answer your question? [How do I select rows from a DataFrame based on column values?](https://stackoverflow.com/questions/17071871/how-do-i-select-rows-from-a-dataframe-based-on-column-values) – CutePoison Nov 07 '22 at 05:15
  • Downvoted - please edit your title to be a question regarding your coding issue. Futher more "voted to close" due to duplicate https://stackoverflow.com/questions/17071871/how-do-i-select-rows-from-a-dataframe-based-on-column-values – CutePoison Nov 07 '22 at 05:17
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 07 '22 at 06:22

1 Answers1

0

Your data that you provided has been saved to a csv file. Added one more line for clarity: U1077 Prawns Biryani. With the help of Pandas I read them, this is how they look:

  user_id                  dish
0   U1077        Prawns Biryani
1   U1077       Chilli Potatoes
2   U1077  Paneer Multani Tikka
3   U1077        Prawns Biryani
4   U1068         Kadaai Paneer
5   U1068           Veg biryani
6   U1068  Mushroom Malai Tikka
7   U1067       Chilli Mushroom
8   U1077         Lacha Paratha

Next, I used explicit loc indexing, where row indices are in square brackets on the left (in this case, this is a Boolean mask, which is obtained from the expression df['user_id'] == 'U1077'), on the right is the name of the column. I also applied unique() at the end to get only unique values if the dish was ordered several times. Result list:

['Prawns Biryani' 'Chilli Potatoes' 'Paneer Multani Tikka' 'Lacha Paratha']

if you remove unique() you will get all rows with the selected 'user_id':

0          Prawns Biryani
1         Chilli Potatoes
2    Paneer Multani Tikka
3          Prawns Biryani
8           Lacha Paratha

code:

import pandas as pd

df = pd.read_csv('dishes.csv', header=0)

print(df)

aaa = df.loc[df['user_id'] == 'U1077', 'dish'].unique()

print(aaa)
inquirer
  • 4,286
  • 2
  • 9
  • 16