十分钟快速上手pandas

本博客主要来源于pandas官网的10 minutes to pandas,后面再慢慢加入更多、更详细的用法。

创建

# 用一维数组创建Series
s = pd.Series([1, 3, 5, np.nan, 6, 8])
# 用二维数组和index、columns来创建 dates
= pd.date_range('20130101', periods=6) df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
# 一列列创建dataframe df2
= pd.DataFrame({'A': 1.,             'B': pd.Timestamp('20130102'),             'C': pd.Series(1, index=list(range(4)), dtype='float32'),             'D': np.array([3] * 4, dtype='int32'),             'E': pd.Categorical(["test", "train", "test", "train"]),             'F': 'foo'})
# 生成的结果
df2=
     A          B    C  D      E    F
0  1.0 2013-01-02  1.0  3   test  foo
1  1.0 2013-01-02  1.0  3  train  foo
2  1.0 2013-01-02  1.0  3   test  foo
3  1.0 2013-01-02  1.0  3  train  foo

视图

df.head()
df.tail(3)
df.index
df.columns
df.to_numpy()
df.describe()  # a quick statistic summary of your data
df.T

# 排序
df.sort_index(axis=1, ascending=False)
df.sort_values(by='B')

选择(索引和切片)

at, loc, iat, iloc

获取单独一列,返回Series,df['A']或者df.A

获取多行,df[0:3] 这里的0和3都是index

通过label来进行选择,df.loc[index1, index2],index1和index2都是

获取一列,返回dataframe,df.loc[]

猜你喜欢

转载自www.cnblogs.com/flyangovoyang/p/11636590.html