数据结构
Series
import numpy as np
import pandas as pd
s = pd.Series([2, 4, np.nan, 8])
print(s)
# 输出
0 2.0
1 4.0
2 NaN
3 8.0
dtype: float64
DataFrame
index(行)等于axis0,columns(列)等于axis1。
import numpy as np
import pandas as pd
dates = pd.date_range('20200101', periods=4)
random = np.random.randn(4, 4)
df = pd.DataFrame(random, index=dates, columns=['c1', 'c2', 'c3', 'c4'])
print(df)
c1 c2 c3 c4
2020-01-01 -1.423157 0.540590 1.752003 -0.368315
2020-01-02 -0.612663 -0.286877 0.696933 -1.793101
2020-01-03 0.082774 0.879219 -0.372299 0.804508
2020-01-04 1.058938 1.488765 -0.151950 -0.940176
#取一列
df['c2']
# 行切片
df[0:3]
df['20200101':'20200103']