How to avoid Python/Pandas creating an index in a saved csv?

How to avoid Python/Pandas creating an index in a saved csv?

Asked on November 12, 2018 in Python.
Add Comment


  • 3 Answer(s)

    This could be simly done by using index=False.

    df.to_csv('your.csv', index=False)
    
    
    Answered on November 12, 2018.
    Add Comment

        There are 2 ways that to handle matters wherever we have a tendency to don’t wish the index to be keep in csv file.

    As others have explicit you’ll use index=False whereas saving your
    dataframe to csv file.

    df.to_csv('file_name.csv',index=False)
    

         Otherwise you will save your dataframe because it is with Associate in Nursing index, and whereasreading you only drop the column unknown zero containing your previous index.Simple!

    df.to_csv(' file_name.csv ')
    df_new = metal.read_csv('file_name.csv').drop(['unnamed 0'],axis=1)
    

    Answered on November 12, 2018.
    Add Comment

    The one of the best answer is by keeping column as index.

    pd.read_csv('filename.csv', index_col='Unnamed: 0')
    
    Answered on November 12, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.