Home > Enterprise >  Update pandas specific row in file without index
Update pandas specific row in file without index

Time:04-05

My data.csv file has no rows, because upon saving I used this code:

data_pd.to_csv("data.csv", index=False)

So now my data file looks like this:

PRICE,SIDE,STATUS
40,UP,NEW
30,DOWN,OLD
80,UP,NEW

There's no "index". I tried codes like iloc, at, and so on, but didn't work.

How can I change the value of a specific row, for example, row 2 - which is "30,DOWN,OLD" - and I want to change column "SIDE" to "UP" on this row 2.

Code:

data_pd = pd.read_csv('data.csv')
data_pd.at[2,'SIDE'] = 'UP'

I see no errors, but doesn't work.

CodePudding user response:

Python default indexing starts from 0.

If you want to update the 2nd row, you should write

data_pd.at[1,'SIDE'] = 'UP'

Your code won't do anything as value for SIDE in the third row whose index is 2 is already UP.

  • Related