惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Fortinet All Blogs
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
小众软件
小众软件
有赞技术团队
有赞技术团队
雷峰网
雷峰网
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
AWS News Blog
AWS News Blog
C
Cisco Blogs
美团技术团队
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
W
WeLiveSecurity
D
DataBreaches.Net
博客园 - 司徒正美
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
Google DeepMind News
Google DeepMind News
T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Vercel News
Vercel News
月光博客
月光博客
T
Tailwind CSS Blog
H
Help Net Security
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
V
V2EX
WordPress大学
WordPress大学
Cyberwarzone
Cyberwarzone
Recent Announcements
Recent Announcements

博客园 - Mr__BRIGHT

Jupyter Notebook导入自定义模块时ImportError Pandas数据处理(2): 数据透视表,行转列、列转行、以及一行生成多行 Win10-PowerShell使用conda activate激活环境无效问题及常用Conda操作 Spring AOP动态代理实现,解决Spring Boot中无法正常启用JDK动态代理的问题 Golang 如何统一处理HTTP请求中的异常捕获 CentOS常用的文件操作命令 win10周年更新后程序各种卡死,进程无法结束怎么破? GIT分支管理模型 - Mr__BRIGHT - 博客园 CentOS访问Windows共享文件夹的方法 GIT FLOW 时序图 - Mr__BRIGHT Hyper-v虚拟机文件VHDX与VHD的格式转换 CentOS详解top命令各个数据的含义 CentOS网络配置 git svn clone时间估算 使用git svn clone迁移svn仓库(保留提交记录) .NET 4.5+项目迁移.NET Core的问题记录 老毛桃u盘装系统制作工具 一位39岁程序员的困惑:知道得越多编程越慢怎么办? 程序员的回归式进化
Pandas数据处理(1): 基础方法整理
Mr__BRIGHT · 2020-08-15 · via 博客园 - Mr__BRIGHT
# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
# %%
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

print('import finished.')

file_path = 'd:\\stock.xlsx'

#读取CSV
#file_path = pd.read_csv(fifa_path, index_col='Date', parse_dates=True)

#读取EXCEL
datafr = pd.read_excel(file_path)

datafr


# %%
#行索引
datafr.index


# %%
#列索引
datafr.columns


# %%
#空值列
datafr.info()


# %%
#显示空值
datafr.isnull()


# %%
#空值填充
datafr.fillna(0)
datafr.fillna({'PARTN':0})


# %%
#表中数值分布
datafr.describe()


# %%
datafr['PRICE'].dtype


# %%
#数据指定列去重,并保留first/last行
datafr.drop_duplicates(subset=['PROD'], keep='last').head()


# %%
#索引列转为数据列
datafr.reset_index()


# %%
#指定列作为索引
datafr.reset_index().set_index('DATE')


# %%
#删除原有索引,并生成新索引
datafr.reset_index(drop=True)


# %%
#装入指定列
datafr[['PROD','QTY']]


# %%
#查找行列索引对应的数据
datafr.iloc[[0,3,5],[0,2,3,4]]


# %%
#查找第一行第一列的数据
datafr.iloc[1,1]


# %%
#行索引查找数据
datafr.loc[12]


# %%
#统计出现次数并降序显示
datafr['PROD'].value_counts(normalize=True,sort=True)


# %%
#取唯一值
datafr['PROD'].unique()


# %%
datafr['PROD'].isin([10,22])


# %%
#树型显示
datafr.stack()


# %%
#宽表转长表
datafr.set_index(['STOCK','PROD']).stack().reset_index()


# %%
#求方差
datafr.var(axis=1)


# %%
#所有数据执行函数
datafr[['QTY','TOTAL']].apply(lambda x:x*100)


# %%
#分组统计/求和,并重置索引
datafr.groupby('PROD').aggregate(['count','sum']).reset_index().head()


# %%
#数据透视表
pd.pivot_table(data=datafr,values=['QTY','TOTAL'],index='PROD',columns='PARTN',aggfunc={'QTY':'sum','TOTAL':'sum'},fill_value=0,margins=True,margins_name='SUM').to_excel(excel_writer='d:\\stock_pivot.xlsx')


# %%
plt.rcParams['font.family'] = ['simhei']
plt.rcParams['figure.autolayout'] = True

fig, axes = plt.subplots(1, 2)
ax1 = axes[0]
ax2 = axes[1]

pt = pd.pivot_table(data=datafr,values=['QTY','TOTAL'],index=['PROD'],aggfunc={'QTY':'sum','TOTAL':'sum'},fill_value=0)

ax1.bar(x=pt.index, height=pt.QTY, edgecolor='k')
ax2.bar(x=pt.index, height=pt.TOTAL)

ax1.set_title('QTY')
ax2.set_title('TOTAL')

for tick in ax1.get_xticklabels():
    tick.set_rotation(40)
for tick in ax2.get_xticklabels():
    tick.set_rotation(40)

plt.tick_params(bottom=False, left=False)
plt.show()


# %%
plt.figure(figsize=(14,6))
plt.title("SALES FOR YEARS")

pt2 = pd.pivot_table(data=datafr,values=['QTY','TOTAL','PRICE'],index=['DATE'],aggfunc={'QTY':np.sum,'TOTAL':np.sum,'PRICE':np.average},fill_value=0)

sns.lineplot(data=pt2)