Reputation: 139
My code:
if __name__ == "__main__":
while True:
df = pd.read_csv('test - Sheet1 (6).csv')
for index, row in df.iterrows():
df['new']=preprocessing(row['prev'])
df.to_csv('today.csv',header=False)
I have some values in my CSV file like this in prev column-
prev
ABC
Abc
BaC
I have pre-processing function that will change the column something like this-
new
abc
abc
bac
But when I am writing it to CSV file, the output I am getting is -
prev | new |
---|---|
ABC | bac |
Abc | bac |
BaC | bac |
it's only storing the last row value. How do I make it like this-
prev | new |
---|---|
ABC | abc |
Abc | abc |
BaC | bac |
Upvotes: 2
Views: 1107
Reputation: 71560
Instead of the loop, just use apply
:
df = pd.read_csv('test - Sheet1 (6).csv')
df['new'] = row['prev'].apply(preprocessing)
df.to_csv('today.csv', header=False)
Iterating through and assigning assigns to the whole column at once, not to that specific index, if you want iterrows
try:
df = pd.read_csv('test - Sheet1 (6).csv')
for index, row in df.iterrows():
df.loc[index, 'new'] = preprocessing(row['prev'])
df.to_csv('today.csv', header=False)
Upvotes: 1