Home > Back-end >  How to add index to a csv file that doesn't have an index using pandas
How to add index to a csv file that doesn't have an index using pandas

Time:12-29

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?

CodePudding user response:

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")
  • Related