Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add index to a csv file that doesn't have an index using pandas

In stack, overflow I see a lot of questions about removing index from dataframes made be to_csv.
However, what I want to do is add an index to an already made csv file with no index.

Here is my file:

How do we add an index to this csv with pandas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you read csv file as dataframe, pandas will automatically generate index. You don’t need to do something else.

So, just read it and write again as below

import pandas as pd

df = pd.read_csv("your_file.csv")
df.to_csv("your_file_to_save.csv")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading