python的agg函数 python中agg函数( 五 )


resample函数 日期重采样:s.resample('M').mean()
TimeGrouper 重组:s.groupby(pd.TimeGrouper('4M')).idxmax()
split 分割函数:temp = df['From_To'].str.split('_', expand=True) True为DataFrame
两个DataFrame拼接用join:df = df.join(temp)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
%matplotlib inline 直接显示
折线图:plt.plot(x,y,color = 'r')
柱状图:plt.bar(x,y)plt.barh(x,y) 多个bar x设置不同 堆积图 bottom设置不同
散点图:plt.scatter(x, y, c=colors, alpha=0.5, s = area)
直方图:plt.hist(a,bins= 20) bin代表分隔的最小单位
plt.legend() 显示图例
for a,b in zip(X+W[i],data[i]):
plt.text(a,b,"%.0f"% b,ha="center",va= "bottom") 添加数据标签
plt.annotate('注释文本',xy=(1, np.sin(1)),xytext=(2, 0.5), fontsize=16,arrowprops=dict(arrowstyle="-")) 添加注释文本
plt.xlabel("Group") x轴标题
plt.ylabel("Num") y轴标题
fig, axes = plt.subplots(nrows=2, ncols=2,facecolor='darkslategray')绘制多个图形
axes[0,0] axes[0,1] axes[1,0] axes[1,1]
pylab.rcParams['figure.figsize'] = (10, 6) # 调整图片大小
动态展示图表
from pyecharts.charts import Bar
from pyecharts import options as opts
** pyecharts 绘图的五个步骤:**
创建图形对象:bar = Bar()
添加绘图数据:bar.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
bar.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
配置系列参数:对标签、线型等的一些设置
配置全局参数:bar.set_global_opts(title_opts=opts.TitleOpts(title="销售情况"))
渲染图片:生成本地 HTML 文件 bar.render("mycharts.html")bar.render()
notebook 渲染:bar.render_notebook()
bar = (Bar()
.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
.set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况"))
)
bar.render_notebook()
柱状图:Bar()
条形图:bar.reversal_axis() #翻转XY轴 , 将柱状图转换为条形图
折线图:from pyecharts.charts import Lineline=Line()
饼图:from pyecharts.charts import Page, PiePie()
转换日期类型:df['order_dt']=pd. to_datetime (df.order_dt,format="%Y%m%d")
将日期转换为月为单位:df['month']=df.order_dt.values. astype('datetime64[M]') 所有日期显示为当月第一天
去除日期单元值:order_diff/ np.timedelta64(1,'D')
过滤部分极值:grouped_user.sum() .query('order_products100') .order_amount
数据透视表:rfm=df.pivot_table( index ='user_id', values =['order_products','order_amount'], aggfunc ={'order_amount':'sum','order_products':'sum'})
map()方法是pandas.series.map()方法, 对DF中的元素级别的操作, 可以对df的某列或某多列
applymap(func)也是DF的属性, 对整个DF所有元素应用func操作
purchase_r=pivoted_counts.applymap(lambda x: 1 if x1 else np.NaN if x==0 else 0)
apply(func)是DF的属性, 对DF中的行数据或列数据应用func操作,也可用于Series
apply(lambda x:x.cumsum()/x.sum())累计占比
apply(lambda x:x/x.sum(),axis=0)每一列中每行数据占比
下周开始进入数据分析思维的课程 , 很期待后面的课程以及项目,加油!
python中range()函数怎么用?。?/h2>range()函数的用法如下:
(1)range(stop)
创建一个(0,stop)之间的整数序列,步长为1 。
(2)range(start,stop)

推荐阅读