disable index pandas data frame

前端 未结 5 1470
既然无缘
既然无缘 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:28

    I have a function that may help some. I combine csv files with a header in the following way in python:

        def combine_csvs(filedict, combined_file):
            files = filedict['files']
            df = pd.read_csv(files[0])
            for file in files[1:]:
                df = pd.concat([df, pd.read_csv(file)])
            df.to_csv(combined_file, index=False)
            return df
    

    It can take as many files as you need. Call this as:

        combine_csvs(dict(files=["file1.csv","file2.csv", "file3.csv"]), 'output.csv')
    

    Or if you are reading the dataframe in python as:

        df = combine_csvs(dict(files=["file1.csv","file2.csv"]), 'output.csv')
    

    The combine_csvs fucntion does not save the indices. If you need the indices use 'index=True' instead.

提交回复
热议问题