I need to replace a value in correspondence of an ID in a.csv file:
ChatId,Color
805525230,black
So if the ID in input is equal to the one in the file my program will replace the Color "black" with the new one. I tried this:
for idx, row in enumerate(df.ChatId):
if str(row) == str(CHAT_ID):
df.loc[1,idx] = BGc
df.to_csv("path")
Assuming you loaded the csv on a dataframe, you could you use some of its functions.
For example: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.replace.html
Alternatively, you could use the loc function:
df.loc[df['ChatId'=='SomeId'], 'Color'] = 'ValueToReplace'
If you need to apply it to a different column, you can 'Color' with the appropriate name.
I think you could even include a list of column names, though I haven't tested it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.