Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: how to edit values in a column of a .csv file?

Tags:

python

pandas

I have a .csv file which looks as follows:link

I want to open this file using pandas and edit the column Coordinate by adding a constant value of 756 to each value in it. Finally, I want the changes to be reflected in the .csv file.

How do I do that?

Edit: What I had been doing is as follows (@EdChum):

df = pd.read_csv('C:/TestBook1.csv')
df = df[['Coordinate','Speed']]

Coord = df['Coordinate']
Coord = Coord + 756

This is where I was going wrong. From here it would have been a messy affair to save changes into the .csv file.

like image 213
TRK Avatar asked Feb 06 '23 11:02

TRK


2 Answers

you can also type:

df["Coordinate"] = df["Coordinate"] + 756
like image 166
ℕʘʘḆḽḘ Avatar answered Feb 13 '23 07:02

ℕʘʘḆḽḘ


@EdChum: Thanks for your comment. It kind of fired me up. I was unnecessarily complicating things for myself. Following is what I did:

df = pd.read_csv('C:/TestBook1.csv')
df = df[['Coordinate','Speed']]

df['Coordinate']+=756
df.to_csv('C:/TestBook1.csv')

Initially I was loading all the values of the column into a variable and trying to find a way to save it. After your comment I thought of experimenting and I am glad that it worked for me.

like image 42
TRK Avatar answered Feb 13 '23 05:02

TRK