Pandas库介绍--统计相关
【Pandas库介绍--统计相关】数据应用上节读取的CSV;
1.值分类汇总
counts = df['hGrade'].value_counts()
print counts
![Pandas库介绍--统计相关](https://img.it610.com/image/info10/da7a2f69e7ca41499c4b6d443887f435.jpg)
文章图片
2.绘制柱形图,并保存
plt = counts.plot(kind='bar').get_figure()
plt
plt.savefig('/Users/baidu/Downloads/plot.png')
![Pandas库介绍--统计相关](https://img.it610.com/image/info10/8ca97ee907bd4ddf81bdaf7e2fa94e9e.jpg)
文章图片
3.数据分组
#数据准备
import pandas as pd
import numpy as np
df = pd.DataFrame({'A':['foo', 'bar','foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B':['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
'C':np.random.randn(8),
'D':np.random.randn(8)})
print df
![Pandas库介绍--统计相关](https://img.it610.com/image/info10/efac31df71ed464894c911321ca8dfdb.jpg)
文章图片
#根据列A进行分组,取每组第一行数据
grouped = df.groupby('A')
print grouped.first()
![Pandas库介绍--统计相关](https://img.it610.com/image/info10/eb0b2ad431f94f50af6b401c91d8ecbc.jpg)
文章图片
#根据多列进行分组A,B列
grouped = df.groupby(['A', 'B'])
print grouped.last()
![Pandas库介绍--统计相关](https://img.it610.com/image/info10/3a089b2feaf94fd0a55a3999550502d1.jpg)
文章图片
根据列进行分组
def get_type(letter):
if letter.lower() in 'abem':
return 'vowel'
else:
return 'consonant'grouped = df.groupby(get_type, axis=1)
print grouped.first()
![Pandas库介绍--统计相关](https://img.it610.com/image/info10/ee102d14dba041919fb61d2a3e5ed29a.jpg)
文章图片
推荐阅读
- Docker应用:容器间通信与Mariadb数据库主从复制
- 太平之莲
- thinkphp|thinkphp 3.2 如何调用第三方类库
- 我正在参加安特思库共读一本书干法。
- Python爬虫|Python爬虫 --- 1.4 正则表达式(re库)
- Apache多路复用模块(MPMs)介绍
- 现役联盟前十怎么排(詹姆斯榜首无悬念!杜兰特库里位置不确定!)
- Android7.0|Android7.0 第三方应用无法访问私有库
- 数据库设计与优化
- 数据库总结语句