Python pandas.DataFrame.from_csv

The challenge is very simple data analysis, where I load the report using api and it comes as a csv file. I am 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 has no indexes inside it, the first column of data I need is ignored by the file frame because it counts as an index column. I wanted to know if there is a way to make the dataframe automatically insert the index column.

+3


source to share


1 answer


Your mistake here was to assume that param index_col=0

means it will not treat your csv as having an index column. It should be index_col=None

, and in fact is the default, so you could leave it blank and it would work:

@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 information refer to the docs

+5


source







All Articles