disable index pandas data frame

前端 未结 5 1466
既然无缘
既然无缘 2021-02-05 05:04

How can I drop or disable the indices in a pandas Data Frame?

I am learning the pandas from the book \"python for data analysis\" and I already know I can use the datafr

5条回答
  •  北海茫月
    2021-02-05 05:29

    I was having a similar issue trying to take a DataFrame from an index-less CSV and write it back to another file.

    I came up with the following:

    import pandas as pd
    import os
    
    def csv_to_df(csv_filepath):
        # the read_table method allows you to set an index_col to False, from_csv does not
        dataframe_conversion = pd.io.parsers.read_table(csv_filepath, sep='\t', header=0, index_col=False)
        return dataframe_conversion
    
    def df_to_excel(df):
        from pandas import ExcelWriter
        # Get the path and filename w/out extension
        file_name = 'foo.xlsx'
        # Add the above w/ .xslx
        file_path = os.path.join('some/directory/', file_name)
        # Write the file out
        writer = ExcelWriter(file_path)
        # index_label + index are set to `False` so that all the data starts on row
        # index 1 and column labels (called headers by pandas) are all on row index 0.
        df.to_excel(writer, 'Attributions Detail', index_label=False, index=False, header=True)
        writer.save()
    

提交回复
热议问题