Shubham Gupta
Shubham Gupta

Reputation: 660

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?

Upvotes: 6

Views: 5023

Answers (2)

msklc
msklc

Reputation: 604

There is 2 option read series from csv file;

  1. pd.Series.from_csv('File_name.csv')

  2. pd.read_csv('File_name.csv', squeeze=True)

My prefer is; using squeeze=True with read_csv

Upvotes: 3

Vipin
Vipin

Reputation: 5223

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.

Upvotes: 3

Related Questions