简体   繁体   中英

Python pandas.DataFrame.from_csv

The task is a very simple data analysis, where I download a report using an api and it comes as a csv file. I have been trying to convert it correctly to a DataFrame using the following code:

@staticmethod
    def convert_csv_to_data_frame(csv_buffer_file):
        data = StringIO(csv_buffer_file)
        dataframe = DataFrame.from_csv(path=data, index_col=0)
        return dataframe

However, since the csv don't have indexes inside it, the first column of the data I need is beeing ignored by the dataframe because it is considered the index column. I wanted to know if there is a way to make the dataframe insert an index column automatically.

Your error here was to assume that param index_col=0 meant that it would not treat your csv as having an index column. This should've been index_col=None and in fact this is the default value so you could have not specified this and it would have worked:

@staticmethod
    def convert_csv_to_data_frame(csv_buffer_file):
        data = StringIO(csv_buffer_file)
        dataframe = DataFrame.from_csv(path=data) # remove index_col param
        return dataframe

For more info consult the docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM