xiaolong
xiaolong

Reputation: 3657

read CSV file into pandas DataFrame, and build datetime index from multiple columns

I have a CSV file like this:

2011    1   10  1000000
2011    1   11  998785
2011    1   12  1002940
2011    1   13  1004815
2011    1   14  1009415
2011    1   18  1011935

I want to read it into a DataFrame object and have a datetime typed index built from the frist 3 colomns. The final DataFrame should look like this:

                     values
datetime(2011,1,10)  1000000
datetime(2011,1,11)  998785
...

How should I do that? Thanks a lot!

Upvotes: 0

Views: 1598

Answers (1)

unutbu
unutbu

Reputation: 880947

import io
import pandas as pd
content = io.BytesIO('''\
2011    1   10  1000000
2011    1   11  998785
2011    1   12  1002940
2011    1   13  1004815
2011    1   14  1009415
2011    1   18  1011935''')

df = pd.read_table(content, sep='\s+', parse_dates=[[0,1,2]], header=None)
df.columns=['date', 'values']
print(df)

yields

                 date   values
0 2011-01-10 00:00:00  1000000
1 2011-01-11 00:00:00   998785
2 2011-01-12 00:00:00  1002940
3 2011-01-13 00:00:00  1004815
4 2011-01-14 00:00:00  1009415
5 2011-01-18 00:00:00  1011935

Upvotes: 3

Related Questions