I have a dataframe with 4 columns , column 0 contains fruits names, column 1 contains no. of orders, column 2 contains customer name, column 4 contains day of delivery.
So If user change any value through a form it should reflect in csv file.
Here I used dataframe to manipulate csv file so column 0 and column 3 cannot be changed by user. So if user want to change number of orders and day of delivery, so to change their order they will enter their ordered fruit name and customer name and other two values which they want to change so it should reflect in csv file.
Here is the code:
0,0,1,2,3
0,Apple,3,customername_1,Monday
1,Apple,15,customername_2,Tuesday
2,Banana,15,customername_3,tuesday
3,Banana,3,customername_4,Wednesday
4,Watermelon,15,customername_5,Monday
5,Strawberry,15,customername_6,Thursday
value1 = "Apple"
value2 = "customer_1"
df = pd.read_csv("symbol.csv", index_col=False)
df.loc[((df["0"] == value1) & (df["2"] == value2)), "1"] = "5"
df.loc[((df["0"] == value1) & (df["2"] == value2)), "3"] = "Thursday"
df.to_csv("symbol.csv", index=False)
csv file I should get:
0,0,1,2,3
0,Apple,5,customer_1,Thursday
1,Apple,15,customername_2,Tuesday
2,Banana,15,customername_3,tuesday
3,Banana,3,customername_4,Wednesday
4,Watermelon,15,customername_5,Monday
5,Strawberry,15,customername_6,Thursday
I am not getting any result from my code. Please help me out. Thanks in advance.
CodePudding user response:
Don't forget to make your code reproducible and remove unnecessary bits to help answer your question.
df = pd.DataFrame([["Apple",3,"customer_1","Monday"],
["Apple",15,"customer_2","Tuesday"],
["Banana",15,"customer_3","Tuesday"]],
columns=['0','1','2','3'])
value1 = "Apple"
value2 = "customer_1"
df.loc[(df.loc[:,'0'] == value1) & (df.loc[:,'2'] == value2), '1'] = 5
df.loc[(df.loc[:,'0'] == value1) & (df.loc[:,'2'] == value2), '3'] = "Thursday"
