Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the csv file already present called input.csv. In that file, we have the 3 headers (filename, Category, Version) under which certain values already exist. I want to know how can I reopen my csv file and input only one of the values multiple times under my Version column such that whatever was written under a column Version gets re-edited by using new input. So suppose under my Version column I had 3 inputs in 3 rows:

VERSION

  55

  66

  88

It gets rewritten by the new input 10 so it will look like:

VERSION

  10

  10

  10

I know normally we input the csv row-wise but this time around I just want to input the column-wise under that specific header VERSION.

1 Answer

0 votes
by (36.8k points)

Solution 1:

With the pandas, you can use:

import pandas as pd

df = pd.read_csv(file)

df['VERSION'] = 10

df.to_csv(file, index=False)

Solution 2: If there is a multiple rows, then you need to use:

df.loc[df.index < 3, ['VERSION']] = 10  

instead of:

df['VERSION'] = 10

 Want to be a master in Data Science? Enroll in this Data Science Courses

Browse Categories

...