数据结构

Series(一维)和DataFrame(二维)

Series

  1. import numpy as np
  2. import pandas as pd
  3. s = pd.Series([2, 4, np.nan, 8])
  4. print(s)
  5. # 输出
  6. 0 2.0
  7. 1 4.0
  8. 2 NaN
  9. 3 8.0
  10. dtype: float64

DataFrame

index(行)等于axis0,columns(列)等于axis1。

  1. import numpy as np
  2. import pandas as pd
  3. dates = pd.date_range('20200101', periods=4)
  4. random = np.random.randn(4, 4)
  5. df = pd.DataFrame(random, index=dates, columns=['c1', 'c2', 'c3', 'c4'])
  6. print(df)
  7. c1 c2 c3 c4
  8. 2020-01-01 -1.423157 0.540590 1.752003 -0.368315
  9. 2020-01-02 -0.612663 -0.286877 0.696933 -1.793101
  10. 2020-01-03 0.082774 0.879219 -0.372299 0.804508
  11. 2020-01-04 1.058938 1.488765 -0.151950 -0.940176
  1. #取一列
  2. df['c2']
  3. # 行切片
  4. df[0:3]
  5. df['20200101':'20200103']

参考

https://www.pypandas.cn/