I'm trying to get all the food-id which has been consumed by a customer. If i give an input of lets say a customerId = C15, then it should return all the food_id's which customer has a link. I tried something.
import datetime
import random
import pandas as pd
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
df = pd.DataFrame({
'food_id':['B1', 'CF', 'C1', 'CF', 'B1', 'IO', 'B1', 'FT', 'BR','CF', 'C1', 'SM', 'IC','B1', 'SM'],
'purch_amt':[150.5, 270.65, 65.26, 110.5, 948.5, 2400.6, 5760, 1983.43, 2480.4, 250.45, 75.29, 3045.6, 500, 500, 250],
'ord_date': ['05-10-2022','09-10-2022','05-10-2022','08-17-2022','10-09-2022','07-27-2022','10-09-2022','10-10-2022','10-10-2022','06-17-2022','07-08-2022','04-25-2022','02-08-2022','08-08-2022','07-09-2022'],
'customer_id':['C31','C31','D35','D31','C35','D31','C35','D31','D35','C31','D35','D35','D35','C35','C38']})
def get_mostly_ordered_food_id():
print('Max food_id ordered')
result = df['food_id'].value_counts().rename_axis('food_id').reset_index(name='counts')
df2 = pd.DataFrame(result)
print(df2.head(3))
def show_data():
result = df.groupby(['customer_id'])
print(result.first())
def test_add_data():
foodID = ['B1', 'CF', 'C1', 'IO', 'FT', 'BR', 'IC', 'SM']
customerID = ['C31','D35','D31','C35','C38']
date = 'Test-Data'
for i in range(0,10):
df.loc[len(df.index)] = [random.choice(customerID), random.choice(foodID), date, 500]
print(df)
get_mostly_ordered_food_id()
If i give an input,say a customer ID then it should return all the food-id which has been consumed. also I should not repeat.
trying to get. e.g. --> input - C15 (which is customer id)
it should return --> B1, C2 (which is food ids) with no duplication. (if any duplication is there, it should throw a warning.)
Thanks
CodePudding user response:
Maybe something like this would work.
# The input customer id
customer_id = "C31"
# Select rows with the given customer id
df_customer = df[df['customer_id'] == customer_id]
# Get the list of unique food ids consumed by the customer
food_ids = df_customer['food_id'].unique()
# Print the list of food ids
print(food_ids)