pythonpandas

How to read a csv file as series instead of dataframe in pandas?


When I try to use x = pandas.Series.from_csv('File_name.csv', header = None) It throws an error saying IndexError: single positional indexer is out-of-bounds.

However, If I read it as dataframe and then extract series, it works fine. x = pandas.read_csv('File_name.csv', header = None)[0]

What could be wrong with first method?


Solution

  • Add index_col=None parameter, seems it is reading entire file in one column and default first column is treated as index.

    Pandas documentation says Series.from_csv is discouraged. read_csv is much more powerful alternative you should use that.