pandas的功能非常强大,支持类似与sql的数据增、删、查、改,并且带有丰富的数据处理函数;
with pd.ExcelWriter('shanghai_%d.xlsx'%iii) as writer:
for i,j in dddit:
j.to_excel(writer,sheet_name=str(i))
#j 为DataFrame类型数据
In [14]: df.head()
Out[14]:
A B C D
2013-01-01 0.469112 -0.282863 -1.509059 -1.135632
2013-01-02 1.212112 -0.173215 0.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
2013-01-04 0.721555 -0.706771 -1.039575 0.271860
2013-01-05 -0.424972 0.567020 0.276232 -1.087401
dates=['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06']
df[0:3] 通过【】切片列 ,axis=0 左闭右开
Out[24]:
A B C D
2013-01-01 0.469112 -0.282863 -1.509059 -1.135632
2013-01-02 1.212112 -0.173215 0.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
In [25]: df['20130102':'20130104'] 两边包含
Out[25]:
A B C D
2013-01-02 1.212112 -0.173215 0.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
2013-01-04 0.721555 -0.706771 -1.039575 0.271860
df['A'] 选择单个列,等同于df.A, 单列的数据类型为series
Out[23]:
2013-01-01 0.469112
2013-01-02 1.212112
2013-01-03 -0.861849
2013-01-04 0.721555
2013-01-05 -0.424972
2013-01-06 -0.673690
Freq: D, Name: A, dtype: float64
df.loc[] 根据数据的索引值(标签) 定位数据
df.loc[dates[0]]
df.loc[:,['A','B']]
df.loc['20130102':'20130104',['A','B']]
A B
2013-01-02 1.212112 -0.173215
2013-01-03 -0.861849 -2.104569
2013-01-04 0.721555 -0.706771
df.loc['20130102',['A','B']]
df.loc[dates[0],'A']
Out[30]: 0.4691122
df.iloc[] 根据数据的位置序号定位数据,而不是索引的值
当入参为1个时,表示纵轴序号值为 y 的行,入参为两个时(x,y),表示横轴上序号为x,纵轴上序号为y的子集
分号 :同列表,左闭右开
d.iloc[1:2,[1,2]]
df.iloc[3:5,0:2]
df.iloc[1:3,:]
df.iloc[1,1]
df.iloc[[1,2,4],[0,2]] 返回行号为1,2,4,列号为0,2的子集
df.iloc[3] 返回序号值为3的行
d.index 返回索引明细
d.dtypes 返回各列(column)的类型及名称
填充空值
d=d.fillna('_')将NA以'_'值替换
排序
通过索引排序,默认是纵轴索引值,升序
df.sort_index(axis=0,ascending=True)
通过数值排序
df.sort_values(by,axis=0,ascending=True)
by可以是单个列标签,也可以是多个列标签的列表
groupby,分组
df2=grouped2.reset_index() 将分组后的索引重新设置为数据
list_data=df2.values.tolist() 将dataframe类型转化为list
df_data['recordat'].apply(lambda x:x.strftime('%Y-%m-%d')) 将函数应用于df的某一列column
pandas的常见统计函数
sum() | 求和 | df/series |
mean() | 求平均值 | df/series |
var() | 计算方差 | df/series |
std() | 计算标准差 | df/series |
df.corr(method='pearson') series1.corr(series2,method='pearson') |
Spearman/Pearson相关系数矩阵 |
df,series method的可选值为pearson,kendall,spearman |
df.cov() --协方差矩阵 series1.cov(series2)--两个series之间的协方差 |
协方差 | df,series |
skew()/kurt() | 计算样本 的偏度(三阶矩)/峰度(四阶矩) | df/series |
describe() | 直接给出数据集的基本统计量,包括均值、标准差、最大值、最小值、分位数 等。 | df/series |
cumsum() | 按顺序返回各数的累计求和的数据集 | df/series |
cumprod() | 返回每次的累乘结果集 | df/series |
cummax() | 一次给出前1,2,3...个数的最大值的数集 | df/series |
cummin() | 一次给出前1,2,3...个数的最小值的数集 | df/series |
cum系列函数是作为df或series对象的方法而出现的,命令格式 为D.cumsum()。D可为df或series。
rolling_系列是pandas的函数,它们使用的格式为pd.rolling_mean(D,K),意思是每K列计算一次均值,滚动计算。
pd.rolling_sum(df,K) | 计算样本每相邻K列的总和 | df |
rolling_mean(df,K) | 计算样本每K列的算数平均数 | |
rolling_var() | 计算数据样本的方差 | |
rolling_std() | 标准差 | |
rolling_corr() | 相关系数矩阵 | |
rolling_cov() | 协方差矩阵 | |
rolling_skew() | 偏度(三阶矩) | |
rolling_kurt() | 峰度(四阶矩) |
读取excel详细入参说明
pandas.read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0, index_col=None,parse_cols=None, parse_dates=False, date_parser=None, na_values=None, thousands=None, convert_float=True,has_index_names=None, converters=None, engine=None, **kwds)
Read an Excel table into a pandas DataFrame
Parameters: |
io : string, file-like object, pandas ExcelFile, or xlrd workbook.
sheetname : string, int, mixed list of strings/ints, or None, default 0 表示读取哪几个工作簿,从0开始
header : int, list of ints, default 0 将某一行设置为标题行,计数从0开始,在跳过行之后重新计数。如skiprows=2,header=2,则将取excel中索引(从0开始计数)为4的行为header,即pd.DataFrame的columns值 表示去掉头两行数据以剩下的数据的索引数(从0开始)为2的行作为header
skiprows : list-like 从开头起,跳过哪几行,默认值为None,等同于0 .【 如果取值skiprows=2,则将从excel中索引数(从0开始计数)为2(包含2)的row处开始读取】
skip_footer : int, default 0。 从尾端起,跳过哪几行, 如等于2,则将跳过最后两行以倒数第三行作为最后一行
index_col : int, list of ints, default None 将某一列设置为索引,从0开始计数
converters : dict, default None 以列名为键,函数为值,对该列的值应用该函数,取结果
parse_cols : int or list, default None 解析哪几列,'A:E'表示解析A列到E列(含)
na_values : list-like, default None 列表,如遇到列表中的值,将其读为na
thousands : str, default None
keep_default_na : bool, default True
verbose : boolean, default False
engine: string, default None
convert_float : boolean, default True
has_index_names : boolean, default None
|
---|---|
Returns: |
parsed : DataFrame or Dict of DataFrames
|