使用 Python 更新 csv 文件的列值
Update column value of the csv file using Python
我的问题是:
客户选择酒店后,请他提供相同的反馈和
根据数据更新 rating.csv 文件 received.And 我如何更新给定反馈给这些文件。
这是我到目前为止尝试过的:
h_id=str(input("Enter Hotel_Id:"))
with open("rating.csv", "r") as fb:
csvreader = csv.reader(fb, delimiter=",")
for row in csvreader:
if h_id in row[0]:
print("Hotel Booked Sucessfully")
f_back=float(input("Please Give Feedback of Hotel you selected outoff 5:"))
我会这样做:
import pandas as pd
#read the hotels table
hotels = pd.read_csv("rating.csv")
h_id=str(input("Enter Hotel_Id:"))
f_back=float(input("Please Give Feedback of Hotel you selected outoff 5:"))
hotels.loc[hotels.Hotel == h_id,"no_of_feedback" ] = hotels.loc[hotels.Hotel == h_id,"no_of_feedback" ]+1
hotels.loc[hotels.Hotel == h_id,"Feedback" ] = f_back
#store updated table
hotels.to_csv("rating.csv")
我的问题是: 客户选择酒店后,请他提供相同的反馈和 根据数据更新 rating.csv 文件 received.And 我如何更新给定反馈给这些文件。
这是我到目前为止尝试过的:
h_id=str(input("Enter Hotel_Id:"))
with open("rating.csv", "r") as fb:
csvreader = csv.reader(fb, delimiter=",")
for row in csvreader:
if h_id in row[0]:
print("Hotel Booked Sucessfully")
f_back=float(input("Please Give Feedback of Hotel you selected outoff 5:"))
我会这样做:
import pandas as pd
#read the hotels table
hotels = pd.read_csv("rating.csv")
h_id=str(input("Enter Hotel_Id:"))
f_back=float(input("Please Give Feedback of Hotel you selected outoff 5:"))
hotels.loc[hotels.Hotel == h_id,"no_of_feedback" ] = hotels.loc[hotels.Hotel == h_id,"no_of_feedback" ]+1
hotels.loc[hotels.Hotel == h_id,"Feedback" ] = f_back
#store updated table
hotels.to_csv("rating.csv")