Jorko12
Jorko12

Reputation: 65

Pandas read_csv() does not work for me

I have data in this format:

DAX 20150728 11173.910156
DAX 20150727 11056.400391
DAX 20150724 11347.450195
DAX 20150723 11512.110352

If I try to read the data with print pd.read_csv('DAX.csv'), I get this: [6246 rows x 1 columns]. Obviously pandas can not manage to read the three columns of data and get all the data in one column.

How can I fix this?

Upvotes: 1

Views: 11843

Answers (2)

paya
paya

Reputation: 11

did you try:

variable = pd.read_csv('DAX.csv')

and then:

print variable

Upvotes: 1

EdChum
EdChum

Reputation: 393933

You need to explicitly pass the separator as the default is comma ',':

In [160]:
t="""DAX 20150728 11173.910156
DAX 20150727 11056.400391
DAX 20150724 11347.450195
DAX 20150723 11512.110352"""
df = pd.read_csv(io.StringIO(t), header=None, sep='\s+',names=['exchange', 'date', 'close'], parse_dates=[1])
df

Out[160]:
  exchange       date         close
0      DAX 2015-07-28  11173.910156
1      DAX 2015-07-27  11056.400391
2      DAX 2015-07-24  11347.450195
3      DAX 2015-07-23  11512.110352

The docs state this.

Upvotes: 1

Related Questions