0

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’)

1 Answer 1

1

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})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help but for what ever reason the file does not get changed even though I can print the correct value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.