Re: How to read csv file into pandas DataFrame with multiple row index level?

1,542 views
Skip to first unread message

Bob

unread,
Apr 22, 2013, 10:47:42 PM4/22/13
to pystat...@googlegroups.com
On Saturday, April 20, 2013 9:15:15 AM UTC-6, Terence wrote:

The original csv file data is like that:

06/04/2011,104.64,105.17
07/04/2011,104.98,105.51
08/04/2011,105.43,105.96
11/04/2011,104.47,104.99

How to either read the csv file into DataFrame and add multiple row index level, or add multiple row index into csv and import into DataFrame as following:

                JAS
      date      bid    ask
06/04/2011   104.64 105.17
07/04/2011   104.98 105.51
08/04/2011   105.43 105.96
11/04/2011   104.47 104.99

Given that no one has responded there must not be an obvious solution to your question. Maybe you could rephrase your question by explaining why you would need mutli-row index level? How would you use this?

Seems to me that multi-row index would be handled by a python variable name not a DataFrame:

data_JAS = pandas.read_csv('JAS.csv', index_col=0, names=['bid', 'ask'], parse_dates=True)

>>> data_JAS
Out[3]: 
               bid     ask
2011-06-04  104.64  105.17
2011-07-04  104.98  105.51
2011-08-04  105.43  105.96
2011-11-04  104.47  104.99

Terence Ng

unread,
Apr 29, 2013, 1:44:58 AM4/29/13
to pystat...@googlegroups.com
It should be:

df = pd.read_csv('JAS.csv', index_col=0, header=None, dayfirst=True, parse_dates=True)
df.columns = pd.MultiIndex.from_tuples([('JAS','bid'),('JAS','ask')])
df.index.name = 'date'
Reply all
Reply to author
Forward
0 new messages