12 Getting Data In/Out
12.1 CSV
In [1]: df.to_csv('foo.csv')
In [2]: pd.read_csv('foo.csv')
Out[2]:
Unnamed: 0 A B C D
0 2000-01-01 -0.218470 -0.061645 -0.723780 0.551225
1 2000-01-02 -0.716236 0.775873 0.379465 -0.567160
2 2000-01-03 -1.259217 -0.218128 1.888207 -0.895857
3 2000-01-04 -1.821452 -0.216532 1.965752 0.093877
.. ... ... ... ... ...
996 2002-09-23 -18.601660 2.473306 2.507465 40.119526
997 2002-09-24 -18.440294 0.302185 2.015355 39.135322
998 2002-09-25 -19.177552 -0.810066 3.468941 40.769274
999 2002-09-26 -19.411661 0.004433 3.718648 39.351233
[1000 rows x 5 columns]
12.2 HDF5
Reading and writing to HDFStores
Writing to a HDF5 Store
In [3]: df.to_hdf('foo.h5','df')
Reading from a HDF5 Store
In [4]: pd.read_hdf('foo.h5','df')
Out[4]:
A B C D
2000-01-01 -0.218470 -0.061645 -0.723780 0.551225
2000-01-02 -0.716236 0.775873 0.379465 -0.567160
2000-01-03 -1.259217 -0.218128 1.888207 -0.895857
2000-01-04 -1.821452 -0.216532 1.965752 0.093877
... ... ... ... ...
2002-09-23 -18.601660 2.473306 2.507465 40.119526
2002-09-24 -18.440294 0.302185 2.015355 39.135322
2002-09-25 -19.177552 -0.810066 3.468941 40.769274
2002-09-26 -19.411661 0.004433 3.718648 39.351233
[1000 rows x 4 columns]
12.3 Excel
Reading and writing to MS Excel
Writing to an excel file
In [5]: df.to_excel('foo.xlsx', sheet_name='Sheet1')
Reading from an excel file
In [6]: pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA'])
Out[6]:
A B C D
2000-01-01 -0.218470 -0.061645 -0.723780 0.551225
2000-01-02 -0.716236 0.775873 0.379465 -0.567160
2000-01-03 -1.259217 -0.218128 1.888207 -0.895857
2000-01-04 -1.821452 -0.216532 1.965752 0.093877
... ... ... ... ...
2002-09-23 -18.601660 2.473306 2.507465 40.119526
2002-09-24 -18.440294 0.302185 2.015355 39.135322
2002-09-25 -19.177552 -0.810066 3.468941 40.769274
2002-09-26 -19.411661 0.004433 3.718648 39.351233
[1000 rows x 4 columns]