pandas 使用总结
import pandas as pd
import numpy as np
## 从字典初始化df
ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings',
'Kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'],
'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2],
'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017],
'Points':[876,789,863,673,741,812,756,788,694,701,804,690]}
df = pd.DataFrame(ipl_data)
print(df)
Team Rank Year Points
0 Riders 1 2014 876
1 Riders 2 2015 789
2 Devils 2 2014 863
3 Devils 3 2015 673
4 Kings 3 2014 741
5 Kings 4 2015 812
6 Kings 1 2016 756
7 Kings 1 2017 788
8 Riders 2 2016 694
9 Royals 4 2014 701
10 Royals 1 2015 804
11 Riders 2 2017 690
print(df.groupby('Team')) ## groupby 返回的对象
<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x7fcbff80a240>
print(df.groupby('Team').groups) ##用groups属性来进行查看每个分组
{'Devils': Int64Index([2, 3], dtype='int64'), 'Kings': Int64Index([4, 5, 6, 7], dtype='int64'), 'Riders': Int64Index([0, 1, 8, 11], dtype='int64'), 'Royals': Int64Index([9, 10], dtype='int64')}
## 对groupby 后的结果进行遍历
grouped = df.groupby('Year')
for name,group in grouped:
print(name)
print(group)
2014
Team Rank Year Points
0 Riders 1 2014 876
2 Devils 2 2014 863
4 Kings 3 2014 741
9 Royals 4 2014 701
2015
Team Rank Year Points
1 Riders 2 2015 789
3 Devils 3 2015 673
5 Kings 4 2015 812
10 Royals 1 2015 804
2016
Team Rank Year Points
6 Kings 1 2016 756
8 Riders 2 2016 694
2017
Team Rank Year Points
7 Kings 1 2017 788
11 Riders 2 2017 690
## 从多个groups中获取单个group
grouped = df.groupby('Year')
print(grouped.get_group(2014))
Team Rank Year Points
0 Riders 1 2014 876
2 Devils 2 2014 863
4 Kings 3 2014 741
9 Royals 4 2014 701
## 使用agg聚合函数计算均值
grouped = df.groupby('Year')
print(grouped['Points'].agg('mean'))
Year
2014 795.25
2015 769.50
2016 725.00
2017 739.00
Name: Points, dtype: float64
## 使用agg聚合函数计算数据条数
grouped = df.groupby('Team')
print(grouped.agg(np.size))
Rank Year Points
Team
Devils 2 2 2
Kings 4 4 4
Riders 4 4 4
Royals 2 2 2
## 使用多个agg聚合函数进行计算
grouped = df.groupby('Team')
print(grouped.agg([np.sum, np.mean, np.std]))
print(grouped['Points'].agg([np.sum, np.mean, np.std]))
print(grouped['Points'].agg({'Points':[np.sum, np.mean, np.std],'Rank':[np.mean]})) ## 分别指定不同的聚合函数
Rank Year Points
sum mean std sum mean std sum mean std
Team
Devils 5 2.50 0.707107 4029 2014.5 0.707107 1536 768.00 134.350288
Kings 9 2.25 1.500000 8062 2015.5 1.290994 3097 774.25 31.899582
Riders 7 1.75 0.500000 8062 2015.5 1.290994 3049 762.25 88.567771
Royals 5 2.50 2.121320 4029 2014.5 0.707107 1505 752.50 72.831998
sum mean std
Team
Devils 1536 768.00 134.350288
Kings 3097 774.25 31.899582
Riders 3049 762.25 88.567771
Royals 1505 752.50 72.831998
Points Rank
sum mean std mean
Team
Devils 1536 768.00 134.350288 768.00
Kings 3097 774.25 31.899582 774.25
Riders 3049 762.25 88.567771 762.25
Royals 1505 752.50 72.831998 752.50
/home/disk1/data/tangshengyu_dxm/tools/env_py36/lib/python3.6/site-packages/ipykernel_launcher.py:5: FutureWarning: using a dict on a Series for aggregation
is deprecated and will be removed in a future version
"""
## grouped数据重新生成dataframe
print(df.groupby('Year')['Team'].apply(len).reset_index()) ## 一级列名
print(df.groupby('Year')['Team'].apply(len).to_frame()) ## 多级列名,列变为索引
Year Team
0 2014 4
1 2015 4
2 2016 2
3 2017 2
Team
Year
2014 4
2015 4
2016 2
2017 2
## 更改聚合后的列名
grouped_df = grouped.agg({'Points':['min','max','mean']})
print(grouped_df.columns)
print(grouped_df.columns.values)
grouped_df.columns = ['_'.join(col_tuple) for col_tuple in grouped_df.columns.values]
grouped_df.reset_index()
MultiIndex(levels=[['Points'], ['min', 'max', 'mean']],
labels=[[0, 0, 0], [0, 1, 2]])
[('Points', 'min') ('Points', 'max') ('Points', 'mean')]
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| Team | Points_min | Points_max | Points_mean | |
|---|---|---|---|---|
| 0 | Devils | 673 | 863 | 768.00 |
| 1 | Kings | 741 | 812 | 774.25 |
| 2 | Riders | 690 | 876 | 762.25 |
| 3 | Royals | 701 | 804 | 752.50 |
## group 后的数据进行transform
grouped = df.groupby('Team')
score = lambda x: (x - x.mean())
print(grouped.transform(score))
Rank Year Points
0 -0.75 -1.5 113.75
1 0.25 -0.5 26.75
2 -0.50 -0.5 95.00
3 0.50 0.5 -95.00
4 0.75 -1.5 -33.25
5 1.75 -0.5 37.75
6 -1.25 0.5 -18.25
7 -1.25 1.5 13.75
8 0.25 0.5 -68.25
9 1.50 -0.5 -51.50
10 -1.50 0.5 51.50
11 0.25 1.5 -72.25
## filter 过滤 (返回满足条件的)
grouped = df.groupby('Team')
print(grouped.filter(lambda x: len(x)>3))
Team Rank Year Points
0 Riders 1 2014 876
1 Riders 2 2015 789
4 Kings 3 2014 741
5 Kings 4 2015 812
6 Kings 1 2016 756
7 Kings 1 2017 788
8 Riders 2 2016 694
11 Riders 2 2017 690
## 每个分组的数据量
grouped = df.groupby('Team')
print(grouped.apply(lambda x: len(x)))
print(type(grouped.apply(lambda x: len(x))))
Team
Devils 2
Kings 4
Riders 4
Royals 2
dtype: int64
<class 'pandas.core.series.Series'>
## 多行字符串组合成一行
print(df)
df_grouped = df.groupby(['Year'])['Team'].apply(';'.join).reset_index()
print(df_grouped)
Team Rank Year Points
0 Riders 1 2014 876
1 Riders 2 2015 789
2 Devils 2 2014 863
3 Devils 3 2015 673
4 Kings 3 2014 741
5 Kings 4 2015 812
6 Kings 1 2016 756
7 Kings 1 2017 788
8 Riders 2 2016 694
9 Royals 4 2014 701
10 Royals 1 2015 804
11 Riders 2 2017 690
Year Team
0 2014 Riders;Devils;Kings;Royals
1 2015 Riders;Devils;Kings;Royals
2 2016 Kings;Riders
3 2017 Kings;Riders
## 一行变多行
def explode(df,tar_col_name):
tar_col_list = [tar_col_name]
rem_col_list = df.columns.difference(tar_col_list)
rem_col_list = list(rem_col_list)
df_new = df.set_index(rem_col_list)
df_explode = pd.DataFrame(df_new[tar_col_name].tolist(),index=df_new.index)
df_explode = df_explode.stack().to_frame()
df_explode.columns = tar_col_list
df_explode = df_explode.reset_index(level= rem_col_list)
return df_explode
df_grouped['Team'] = df_grouped['Team'].apply(lambda s:s.split(';')) ## 先split得到list
print(df_grouped)
explode(df_grouped,'Team')
Year Team
0 2014 [Riders, Devils, Kings, Royals]
1 2015 [Riders, Devils, Kings, Royals]
2 2016 [Kings, Riders]
3 2017 [Kings, Riders]
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| Year | Team | |
|---|---|---|
| 0 | 2014 | Riders |
| 1 | 2014 | Devils |
| 2 | 2014 | Kings |
| 3 | 2014 | Royals |
| 0 | 2015 | Riders |
| 1 | 2015 | Devils |
| 2 | 2015 | Kings |
| 3 | 2015 | Royals |
| 0 | 2016 | Kings |
| 1 | 2016 | Riders |
| 0 | 2017 | Kings |
| 1 | 2017 | Riders |
# 将多列合并成一列
data = [['Alex', 10, 150], ['Bob', 12, 153], ['Clarke', 13, 160], ['Tom', 12, 160]]
df = pd.DataFrame(data, columns=['Name', 'Age', 'Stature'])
print(df)
df_new = df['Age'].astype(str) +'-'+ df['Stature'].astype(str)
print(df_new)
Name Age Stature
0 Alex 10 150
1 Bob 12 153
2 Clarke 13 160
3 Tom 12 160
0 10-150
1 12-153
2 13-160
3 12-160
dtype: object
## 一列拆分成多列
ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings',
'Kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'],
'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2],
'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017],
'Points':[876,789,863,673,741,812,756,788,694,701,804,690]}
df = pd.DataFrame(ipl_data)
df_grouped = df.groupby(['Year'])['Team'].apply(';'.join).reset_index()
print(df_grouped)
df_grouped['Team'].str.split(';', expand=True)
Year Team
0 2014 Riders;Devils;Kings;Royals
1 2015 Riders;Devils;Kings;Royals
2 2016 Kings;Riders
3 2017 Kings;Riders
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 0 | Riders | Devils | Kings | Royals |
| 1 | Riders | Devils | Kings | Royals |
| 2 | Kings | Riders | None | None |
| 3 | Kings | Riders | None | None |
def df2libsvm(df,missing_value='-9999'):
re_list=[]
length=len(df)
for i in range(length):
row_i=df.iloc[i]
row_dict=row_i.to_dict()
row_list=[]
for key in row_dict:
if row_dict[key]==missing_value:
continue
row_list.append('%s:%s'%(key,str(row_dict[key])))
re_list.append(row_list)
return re_list
def libsvm2df():
"""
mydict = [{'b': 2, 'c': 3, 'd': 4},
... {'a': 100, 'c': 300, 'd': 400},
... {'a': 1000, 'b': 2000, 'c': 3000}]
df=pd.DataFrame(mydict)
"""
def calcu_iv(df,feat_col,label_col,good,bad):
import numpy as np
def f(x,label_col,good,bad):
d = {}
d['bin_bad_cnt'] = (x[label_col]==bad).sum()
d['bin_good_cnt'] = (x[label_col]==good).sum()
return pd.Series(d, index=['bin_good_cnt', 'bin_bad_cnt'])
df_woe = df.groupby(feat_col).apply(f,label_col=label_col,good=good,bad=bad).reset_index()
all_good_cnt = df_woe.bin_good_cnt.sum()
all_bad_cnt = df_woe.bin_bad_cnt.sum()
if all_bad_cnt==0:
all_bad_cnt=1
if all_good_cnt==0:
all_good_cnt=1
df_woe = df_woe.replace({'bin_bad_cnt': {0: 0.1}})
df_woe = df_woe.replace({'bin_good_cnt': {0: 0.1}})
df_woe['distribution_good'] = df_woe['bin_good_cnt']/float(all_good_cnt)
df_woe['distribution_bad'] = df_woe['bin_bad_cnt']/float(all_bad_cnt)
df_woe['WoE'] = np.log(df_woe['distribution_good']/df_woe['distribution_bad'])
df_woe['IV'] = df_woe['WoE'] * (df_woe['distribution_good'] - df_woe['distribution_bad'])
df_woe_inf = df_woe[df_woe['WoE']==np.inf]
iv = df_woe['IV'].sum()
return iv,df_woe
pandas 使用总结的更多相关文章
- pandas基础-Python3
未完 for examples: example 1: # Code based on Python 3.x # _*_ coding: utf-8 _*_ # __Author: "LEM ...
- 10 Minutes to pandas
摘要 一.创建对象 二.查看数据 三.选择和设置 四.缺失值处理 五.相关操作 六.聚合 七.重排(Reshaping) 八.时间序列 九.Categorical类型 十.画图 十一 ...
- 利用Python进行数据分析(15) pandas基础: 字符串操作
字符串对象方法 split()方法拆分字符串: strip()方法去掉空白符和换行符: split()结合strip()使用: "+"符号可以将多个字符串连接起来: join( ...
- 利用Python进行数据分析(10) pandas基础: 处理缺失数据
数据不完整在数据分析的过程中很常见. pandas使用浮点值NaN表示浮点和非浮点数组里的缺失数据. pandas使用isnull()和notnull()函数来判断缺失情况. 对于缺失数据一般处理 ...
- 利用Python进行数据分析(12) pandas基础: 数据合并
pandas 提供了三种主要方法可以对数据进行合并: pandas.merge()方法:数据库风格的合并: pandas.concat()方法:轴向连接,即沿着一条轴将多个对象堆叠到一起: 实例方法c ...
- 利用Python进行数据分析(9) pandas基础: 汇总统计和计算
pandas 对象拥有一些常用的数学和统计方法. 例如,sum() 方法,进行列小计: sum() 方法传入 axis=1 指定为横向汇总,即行小计: idxmax() 获取最大值对应的索 ...
- 利用Python进行数据分析(8) pandas基础: Series和DataFrame的基本操作
一.reindex() 方法:重新索引 针对 Series 重新索引指的是根据index参数重新进行排序. 如果传入的索引值在数据里不存在,则不会报错,而是添加缺失值的新行. 不想用缺失值,可以用 ...
- 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍
一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...
- pandas.DataFrame对行和列求和及添加新行和列
导入模块: from pandas import DataFrame import pandas as pd import numpy as np 生成DataFrame数据 df = DataFra ...
- pandas.DataFrame排除特定行
使用Python进行数据分析时,经常要使用到的一个数据结构就是pandas的DataFrame 如果我们想要像Excel的筛选那样,只要其中的一行或某几行,可以使用isin()方法,将需要的行的值以列 ...
随机推荐
- kettle高级教程-自动同步
KETTLE4个工作中有用的复杂实例--2.两表数据比较,比较后自动同步(部门.单位数据同步) 二.两表数据比较核对,核对后自动同步至目标数据表 目标:比较t_bm表的数据和t_bm_target表的 ...
- postman 的基础使用篇(一)
简介 postman 是接口协作开发的优秀平台,可以用来进行接口设计.构建和调试,之前以chrome插件的形式,现在也有原生APP. 安装 Native app 通过链接 download page ...
- k8s-手动安装
预先准备信息 以下列节点数与规格来进行部署 Kubernetes 集群,操作系统CentOS 7.x IP Address Role CPU Memory 192.168.2.10 node10 4 ...
- Django之models高级进阶技术详解
目录 一.常用字段 1.AutoField 2.IntegerField 3.CharField 4.自定义及使用char 5.DateField 6.DateTimeField 二.字段合集 三.字 ...
- 字符串String类常见算法题
1.将一个字符串进行反转.将字符串中指定部分进行反转. public class StringDemo { //方式一:转换为char[] public String reverse(String s ...
- Spring中bean的实例化过程
1.从缓存中.优先从一级缓存中拿,有则返回. 如果没有,则从二级缓存中获取,有则返回. 如果二级缓存中拿不到,则从三级缓存中拿,能拿到,则从三级缓存中删除,移到二级缓存. 如果三级缓存也没有,则返回n ...
- sqlachemy查询对象转化成字典/json使用
https://www.cnblogs.com/sanduzxcvbnm/p/10220718.html
- python 自定义expection
class PricePolicyInvalid(Exception): def __init__(self, msg): self.msg = msg
- [洛谷P4617] [COCI2017-2018#5] Planinarenje
Description \(Mirko\) 和 \(Slavko\) 喜欢一起去远足. \(Mirko\) 偏好攀登山峰,而 \(Slavko\) 偏爱山谷.因此每次他们登上一座山峰后,\(Slavk ...
- ii
char a[10], b[10], c[10], d[10],e[10],f[10],g[10],h[10]; scanf("%s %s %s %s", a, b, c, d); ...