Reputation: 129
Inside of my tag_data.csv file I want to change the column for “Posts” value from “4532” to “1234” in the the row that contains “metal “
My csv file looks like this:
Tag Posts
0 plastic 2456
0 paper 3449
0 metal 4532
0 other 8722
The output should be the file updated and saved to look like this:
Tag Posts
0 plastic 2456
0 paper 3449
0 metal 1234
0 other 8722
This is what I’ve been trying:
read_csv = pd.read_csv(‘tag_data.csv’)
read_csv.loc[read_csv.Tag == ‘metal’, ‘Posts’], [‘Posts’] = 123.to_csv(‘tag_data.csv’)
Upvotes: 0
Views: 81
Reputation: 4660
It looks like you're trying to do too much in one line, this should do the value replacement:
read_csv.loc[(read_csv["Tag"]=="metal"), "Posts"] = 1234
Alternatively, you can use replace
with a dictionary of values:
read_csv["Posts"] = read_csv["Posts"].replace({4532: 1234})
Upvotes: 1