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

推荐订阅源

N
News and Events Feed by Topic
D
Docker
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
F
Full Disclosure
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
L
LangChain Blog
H
Help Net Security
B
Blog
T
Tailwind CSS Blog
V
V2EX
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
美团技术团队
A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
I
InfoQ
Project Zero
Project Zero
I
Intezer
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Securelist
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 叶小钗
S
Security Affairs
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
The Hacker News
The Hacker News

博客园 - 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)