table 和 pivot_table
来源:互联网 发布:卓越工具箱淘宝客软件 编辑:程序博客网 时间:2024/06/16 14:27
透视表在处理时序问题上很方便,处理完后可转换为常规table,便于统计。举例说明:
class_id dates sale_num0 1001 20150101 31 1001 20150102 62 1001 20150103 7...In[]:data = pd.pivot_table(mid_num,index=['class_id'],values=['sale_num'],columns=['dates'])In[]:df = data.iloc[0:3,0:3]In[]:dfOut[21]: sale_numdates 20150101 20150102 20150103class_id1001 3.0 6.0 7.01002 0.0 0.0 0.01004 1.0 3.0 3.0In[]:df.stack()Out[26]: sale_numclass_id dates 1001 20150101 3.0 20150102 6.0 20150103 7.01002 20150101 0.0 20150102 0.0 20150103 0.01004 20150101 1.0 20150102 3.0 20150103 3.0In[]:df.stack().reset_index()Out[28]: class_id dates sale_num0 1001 20150101 3.01 1001 20150102 6.02 1001 20150103 7.03 1002 20150101 0.04 1002 20150102 0.05 1002 20150103 0.06 1004 20150101 1.07 1004 20150102 3.08 1004 20150103 3.0
In[]:df.unstack()Out[27]: dates class_idsale_num 20150101 1001 3.0 1002 0.0 1004 1.0 20150102 1001 6.0 1002 0.0 1004 3.0 20150103 1001 7.0 1002 0.0 1004 3.0In[]: m4_md_num 日期 20150501 20150502 20150503编码 10 17.0 10.0 5.011 7.0 0.0 1.012 145.0 32.0 29.0In[]:df.index.names=[None]In[]:dfOut[206]: m4_md_num 日期 20150501 20150502 2015050310 17.0 10.0 5.011 7.0 0.0 1.012 145.0 32.0 29.0# data.unstack().stack() 进行还原
2、处理pivot_table,去掉索引:
(1)法一
In[]:df m4_md_num 日期 20150501 20150502 20150503编码 10 17.0 10.0 5.011 7.0 0.0 1.012 145.0 32.0 29.0In[]:df.xs('m4_md_num',axis=1,drop_level=True)Out[174]: 日期 20150501 20150502 20150503编码 10 17.0 10.0 5.011 7.0 0.0 1.012 145.0 32.0 29.0In[]:df.reset_index()Out[175]: 日期 编码 20150501 20150502 201505030 10 17.0 10.0 5.01 11 7.0 0.0 1.02 12 145.0 32.0 29.0In[]:df.to_csv('df.csv',index=False)
(2)法二
In[]:df.columns = df.columns.droplevel()In[]:df.reset_index()In[]:df.to_csv('df2.csv')
3、行转列
# 行转列Out[32]: class_id 2015-01-01 2015-01-02 2015-01-03 2015-01-040 1001 3.0 6.0 7.0 5.01 1002 0.0 0.0 0.0 0.02 1004 1.0 3.0 3.0 2.03 1101 1.0 0.0 0.0 0.0In[]:pd.melt(df,id_vars=['class_id'],value_vars=df.columns[1:],var_name='dates',value_name='sale_num')Out[34]: class_id dates sale_num0 1001 2015-01-01 3.01 1002 2015-01-01 0.02 1004 2015-01-01 1.03 1101 2015-01-01 1.04 1102 2015-01-01 0.05 1201 2015-01-01 64.0
4、
阅读全文
0 0
- table 和 pivot_table
- pandas中groupby和pivot_table(数据透视表)
- Visualizations&plot&pivot_table
- pd.pivot_table的作用
- pandas pivot_table() 按日期分多列数据
- Python中的交叉分析pivot_table
- Python中的结构分析pivot_table
- delete table 和 truncate table
- object table 和storage table
- delete table 和 truncate table
- delete table和truncate table
- Pandas透视表(pivot_table)详解
- compution average in python: mean(),pivot_table()
- python3的数据透视表pivot_table
- Pandas透视表(pivot_table)详解
- pivot_table() got an unexpected keyword argument 'rows'
- python——pandas的pivot_table使用
- Pandas透视表(pivot_table)详解
- 多渠道打包方法简介
- 处理Gradle中的这个文件下载慢的问题
- 4用于cifar10的卷积神经网络-4.20relu, relu6和softplus激活函数的输出汇总分析
- 1. Hello Go(Go Tutorial)
- JNI错误之:error: base operand of '->' has non-pointer type '_JNIEnv'
- table 和 pivot_table
- 浅析大端小端
- 【Scikit-Learn 中文文档】十九:高斯混合模型
- qml:c++与qml同时需要实现国际化
- linux用cat命令进入一个很大的文件,如何退出?
- VMware Workstation虚拟机 12.5.7安装
- HDU2012
- 111. Minimum Depth of Binary Tree
- python