2013-10-04 57 views
3

是否有可能閱讀在此格式的CSV文件:熊貓:讀取時間序列數據的CSV與「列」標頭,如行元件

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 

成這樣結束了一個數據幀:

   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/O文檔(http://pandas.pydata.org/pandas-docs/dev/io.html

回答

13

爲什麼不重塑(支點)後您在數據幀讀?

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