Home > front end >  Pandas add new column in csv and save
Pandas add new column in csv and save

Time:03-18

I have code like:

import pandas as pd

df = pd.read_csv('file.csv')

for id1, id2 in zip(df.iterrows(),df.loc[1:].iterrows()):
    id1[1]['X_Next'] = id2[1]['X']

as you see, I need for each row to have next row's column value. Iteration looks good, but I dunno how to save it bvack to csv file. Can someone help me ? thanks!

CodePudding user response:

IIUC use Series.shift:

df = pd.read_csv('file.csv')
df['X_Next'] = df['X'].shift(-1)
df.to_csv('file1.csv', index=False)
  • Related