数据读入、清洗、准备、图表呈现等
1 2 3 4 5
| pip install pandas import pandas as pd
pd.options.display.max_rows = 10 pd.options.display.max_columns = 20
|
Series 一维数组与索引名称捆绑
DataFrame 二维数组,行列索引的捆绑
创建
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| se_.values se_.index
df_.index df_.values df_.columns df_.dtypes
in_.ndim in_.shape in_.dtype in_.size
|
1 2 3 4 5 6 7 8
| pd.Series()
pd.DataFrame()
df_ = pd.DataFrame({'se1_': se1_, 'se2_': se2_})
pd.Index()
|
导入文件
1 2 3 4 5 6
| pd.read_excel() pd.read_csv() pd.read_table()
pd.read_sql(sql, con, ...)
|
保存数据
1 2 3 4
| df_.to_csv(...) df_.to_excel(...) df_.to_sql(...)
|
查看数据
1 2 3 4 5
| df_.head() df.tail() df_.info() df_.shape() df_.describe() pd.value_counts()
|
基本操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| df_.columns = 新列名 df_.rename(新旧名称字典, ...)
df_.列名 df_[列名] df_.[[列名1, 列名2]]
df_.drop() del df_[]
df_[new_column] = pd.Series() df_[new_column] = df_[column1] + df_[column2]
df_.assign(...) df_.insert(...) df_.replace(...)
|
索引&切片
1 2 3 4 5 6 7 8 9 10 11
| df_.set_index(...)
df_.reset_index(...)
df_.index.name = ''
df_.reindex(...)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
se_ = pd.Series(...) se_['a', 'b'] se_[data > 3]
se_[] se_.loc() se_.iloc()
df_[col_] df_[[col1_, col2_]] df_.loc[] df_.iloc[]
|
1 2 3 4 5 6 7 8 9 10 11 12
| df.isin()
df_[df_.isin({ 'height': [1.83, 1.81], 'weight': [83.0, 81.0] })]
df_.query(...)
df_.query("height < 1.82 and weight > 82.0")
|
排序
1 2
| df_.sort_values(...) df_.sort_index(...)
|
运算
1 2 3
| df_.apply(...)
df_.体重.apply(math.sqrt)
|
其他操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| pd.get_dummies(...)
pd.cut(...) pd.qcut(...)
df_.groupby(...)
dfg_ = df_.groupby("年龄") dfg_.groups dfg_.describe() dfg_.get_group("指定分的组").mean() ...[一些计算函数] dfg_.agg(mean...[一些计算函数])
df_.pivot_table() pd.crosstab()
df_.stack(...) df_.unstack(...) df_.T
df1_.append(df2_) pd.merge(...) pd.concat(...)
df_.shift()
|
处理缺失值
1 2 3 4 5 6 7 8
|
df_.isnull() df_.isna() df_.notnull() df_.notna() df_.any() df_.all()
df_.fillna(...) df_.dropna(...)
|
数据查重
1 2 3
| df_.duplicated() df_.drop_duplicates() df_[~df_.duplicated()]
|
日期时间
1 2 3 4 5 6 7 8 9 10 11 12 13
| pd.Timestamp() pd.Peroid()
pd.Timestamp("2030-1-1")
pd.to_datatime(...)
pd.Datatimeindex() pd.data_range() pd.date_range() pd.bdate_range()
df_.resample()
|
数据图形展示
1 2 3
| df_.plot(...)
.plot.line .bar .pie .barh .hist .box .kde .density .area .scatter .hexbin
|
参考学习
https://www.runoob.com/pandas/pandas-tutorial.html
https://pandas.liuzaoqi.com/intro.html