目录
当前位置: 首页 > 文档资料 > Pandas 官方教程 >

十分钟搞定 Pandas - 十二、 导入和保存数据

优质
小牛编辑
137浏览
2023-12-01

CSV

参考:写入 CSV 文件

1、 写入 csv 文件:

  1. In [141]: df.to_csv('foo.csv')

2、 从 csv 文件中读取:

  1. In [142]: pd.read_csv('foo.csv')
  2. Out[142]:
  3. Unnamed: 0 A B C D
  4. 0 2000-01-01 0.266457 -0.399641 -0.219582 1.186860
  5. 1 2000-01-02 -1.170732 -0.345873 1.653061 -0.282953
  6. 2 2000-01-03 -1.734933 0.530468 2.060811 -0.515536
  7. 3 2000-01-04 -1.555121 1.452620 0.239859 -1.156896
  8. 4 2000-01-05 0.578117 0.511371 0.103552 -2.428202
  9. 5 2000-01-06 0.478344 0.449933 -0.741620 -1.962409
  10. 6 2000-01-07 1.235339 -0.091757 -1.543861 -1.084753
  11. .. ... ... ... ... ...
  12. 993 2002-09-20 -10.628548 -9.153563 -7.883146 28.313940
  13. 994 2002-09-21 -10.390377 -8.727491 -6.399645 30.914107
  14. 995 2002-09-22 -8.985362 -8.485624 -4.669462 31.367740
  15. 996 2002-09-23 -9.558560 -8.781216 -4.499815 30.518439
  16. 997 2002-09-24 -9.902058 -9.340490 -4.386639 30.105593
  17. 998 2002-09-25 -10.216020 -9.480682 -3.933802 29.758560
  18. 999 2002-09-26 -11.856774 -10.671012 -3.216025 29.369368
  19. [1000 rows x 5 columns]

HDF5

参考:HDF5 存储

1、 写入 HDF5 存储:

  1. In [143]: df.to_hdf('foo.h5','df')

2、 从 HDF5 存储中读取:

  1. In [144]: pd.read_hdf('foo.h5','df')
  2. Out[144]:
  3. A B C D
  4. 2000-01-01 0.266457 -0.399641 -0.219582 1.186860
  5. 2000-01-02 -1.170732 -0.345873 1.653061 -0.282953
  6. 2000-01-03 -1.734933 0.530468 2.060811 -0.515536
  7. 2000-01-04 -1.555121 1.452620 0.239859 -1.156896
  8. 2000-01-05 0.578117 0.511371 0.103552 -2.428202
  9. 2000-01-06 0.478344 0.449933 -0.741620 -1.962409
  10. 2000-01-07 1.235339 -0.091757 -1.543861 -1.084753
  11. ... ... ... ... ...
  12. 2002-09-20 -10.628548 -9.153563 -7.883146 28.313940
  13. 2002-09-21 -10.390377 -8.727491 -6.399645 30.914107
  14. 2002-09-22 -8.985362 -8.485624 -4.669462 31.367740
  15. 2002-09-23 -9.558560 -8.781216 -4.499815 30.518439
  16. 2002-09-24 -9.902058 -9.340490 -4.386639 30.105593
  17. 2002-09-25 -10.216020 -9.480682 -3.933802 29.758560
  18. 2002-09-26 -11.856774 -10.671012 -3.216025 29.369368
  19. [1000 rows x 4 columns]

Excel

参考:MS Excel

1、 写入excel文件:

  1. In [145]: df.to_excel('foo.xlsx', sheet_name='Sheet1')

2、 从excel文件中读取:

  1. In [146]: pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA'])
  2. Out[146]:
  3. A B C D
  4. 2000-01-01 0.266457 -0.399641 -0.219582 1.186860
  5. 2000-01-02 -1.170732 -0.345873 1.653061 -0.282953
  6. 2000-01-03 -1.734933 0.530468 2.060811 -0.515536
  7. 2000-01-04 -1.555121 1.452620 0.239859 -1.156896
  8. 2000-01-05 0.578117 0.511371 0.103552 -2.428202
  9. 2000-01-06 0.478344 0.449933 -0.741620 -1.962409
  10. 2000-01-07 1.235339 -0.091757 -1.543861 -1.084753
  11. ... ... ... ... ...
  12. 2002-09-20 -10.628548 -9.153563 -7.883146 28.313940
  13. 2002-09-21 -10.390377 -8.727491 -6.399645 30.914107
  14. 2002-09-22 -8.985362 -8.485624 -4.669462 31.367740
  15. 2002-09-23 -9.558560 -8.781216 -4.499815 30.518439
  16. 2002-09-24 -9.902058 -9.340490 -4.386639 30.105593
  17. 2002-09-25 -10.216020 -9.480682 -3.933802 29.758560
  18. 2002-09-26 -11.856774 -10.671012 -3.216025 29.369368
  19. [1000 rows x 4 columns]