简体   繁体   中英

Pandas: Read a CSV of timeseries data with 'column' header as row element

Is it possible to read a CSV file in this format:

2013-01-01,A,1
2013-01-02,A,2
2013-01-03,A,3
2013-01-04,A,4
2013-01-05,A,5
2013-01-01,B,1
2013-01-02,B,2
2013-01-03,B,3
2013-01-04,B,4
2013-01-05,B,5

into a DataFrame that ends up like this:

             A   B
2013-01-01   1   1
2013-01-02   2   2
2013-01-03   3   3
2013-01-04   4   4
2013-01-05   5   5

I couldn't see anything in the I/O docs ( http://pandas.pydata.org/pandas-docs/dev/io.html )

Why not reshape (pivot) after you've read in the DataFrame?

In [1]: df = pd.read_csv('foo.csv', sep=',', parse_dates=[0], header=None,
                         names=['Date', 'letter', 'value'])

In [2]: df
Out[2]: 
                 Date letter  value
0 2013-01-01 00:00:00      A      1
1 2013-01-02 00:00:00      A      2
2 2013-01-03 00:00:00      A      3
3 2013-01-04 00:00:00      A      4
4 2013-01-05 00:00:00      A      5
5 2013-01-01 00:00:00      B      1
6 2013-01-02 00:00:00      B      2
7 2013-01-03 00:00:00      B      3
8 2013-01-04 00:00:00      B      4
9 2013-01-05 00:00:00      B      5

In [3]: df.pivot(index='Date', columns='letter', values='value')
Out[3]:
letter      A  B
Date            
2013-01-01  1  1
2013-01-02  2  2
2013-01-03  3  3
2013-01-04  4  4
2013-01-05  5  5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM