1.pandas中的列的分位数

# 查看列的分位数
import pandas as pd
# set columns type
my_df['col'] = my_df['col'].astype(np.float64) # computations for 4 quantiles : quartiles
bins_col = pd.qcut(my_df['col'], 4)
bins_col_label = pd.qcut(my_df['col'], 4).labels

分位数

2.多重聚合(组函数)

# 多重聚合(组函数)
# columns settings
grouped_on = 'col_0' # ['col_0', 'col_2'] for multiple columns
aggregated_column = 'col_1' ### Choice of aggregate functions
## On non-NA values in the group
## - numeric choice :: mean, median, sum, std, var, min, max, prod
## - group choice :: first, last, count
# list of functions to compute
agg_funcs = ['mean', 'max'] # compute aggregate values
aggregated_values = my_df.groupby(grouped_on)[aggregated_columns].agg(agg_funcs) # get the aggregate of group
aggregated_values.ix[group]

多重聚合

3.使用自定义函数进行聚合

# 使用自定义函数进行聚合
# columns settings
grouped_on = ['col_0']
aggregated_columns = ['col_1'] def my_func(my_group_array):
return my_group_array.min() * my_group_array.count() ## list of functions to compute
agg_funcs = [my_func] # could be many # compute aggregate values
aggregated_values = my_df.groupby(grouped_on)[aggregated_columns].agg(agg_funcs)

自定义函数进行聚合

4.在聚合的dataframe上使用apply

在聚合中使用apply

# 在聚合的dataframe上使用apply
# top n in aggregate dataframe
def top_n(group_df, col, n=2):
bests = group_df[col].value_counts()[:n]
return bests # columns settings
grouped_on = 'col_0'
aggregated_column = 'col' grouped = my_df.groupby(grouped_on)
groups_top_n = grouped.apply(top_n, aggregated_column, n=3)

5.移动平均

# 移动平均
import numpy as np ret = np.cumsum(np.array(X), dtype=float)
ret[w:] = ret[w:] - ret[:-w]
result = ret[w - 1:] / w # X: array-like
# window: int

移动平均

6.组数据的基本信息

# 组数据的基本信息
# columns settings
grouped_on = 'col_0' # ['col_0', 'col_1'] for multiple columns
aggregated_column = 'col_1' ### Choice of aggregate functions
## On non-NA values in the group
## - numeric choice : mean, median, sum, std, var, min, max, prod
## - group choice : first, last, count
## On the group lines
## - size of the group : size
aggregated_values = my_df.groupby(grouped_on)[aggregated_column].mean()
aggregated_values.name = 'mean' # get the aggregate of group
aggregated_values.ix[group]

组数据的基本信息

7.数据组的遍历

数据组的遍历

# 数据组的遍历
# columns settings
grouped_on = 'col_0' # ['col_0', 'col_1'] for multiple columns grouped = my_df.groupby(grouped_on) i = 0
for group_name, group_dataframe in grouped:
if i > 10:
break
i += 1
print(i, group_name, group_dataframe.mean()) ## mean on all numerical columns

8.最大互信息数

# 最大互信息数
import numpy as np matrix = np.transpose(np.array(X)).astype(float)
mine = MINE(alpha=0.6, c=15, est="mic_approx")
mic_result = []
for i in matrix[1:]:
mine.compute_score(t_matrix[0], i)
mic_result.append(mine.mic())
return mic_result

最大互信息数

9.pearson相关系数

import numpy as np

matrix = np.transpose(np.array(X))
np.corrcoef(matrix[0], matrix[1])[0, 1] # X: array-like
# https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.corrcoef.html

pearson相关系数

10.自定义聚合函数

# 自定义聚合函数
def zscore(x):
return (x - x.mean()) / x.std() my_df['zscore_col'] = my_df.groupby(grouped_on)[aggregated_column].transform(zscore)

自定义聚合函数

11.标准聚合使用groupby

# 标准聚合使用groupby
# columns settings
grouped_on = 'col_1'
aggregated_column = 'col_0' ### Choice of aggregate functions
## On non-NA values in the group
## - numeric choice : mean, median, sum, std, var, min, max, prod
## - group choice : first, last, count
my_df['aggregate_values_on_col'] = my_df.groupby(grouped_on)[aggregated_column].transform(lambda v: v.mean())

标准聚合使用groupby

12.使用自定义函数设值

# 使用自定义函数设值
def to_log(v):
try:
return log(v)
except:
return np.nan
my_df['new_col'] = my_df['col_0'].map(to_log)

使用自定义函数设值

13.使用复杂函数设值

# 使用复杂的函数设值
import numpy as np
def complex_formula(col0_value, col1_value):
return "%s (%s)" % (col0_value, col1_value) my_df['new_col'] = np.vectorize(complex_formula)(my_df['col_0'], my_df['col_1'])

使用复杂函数设值

14.使用字典dict设值

# 使用字典dict设值
gender_dict={'男':1,'女':2}
df['gender'] = df['gender'].map(gender_dict)

使用字典设值

参考信息:https://www.kesci.com/

pandas高级操作总结的更多相关文章

  1. 数据分析06 /pandas高级操作相关案例:人口案例分析、2012美国大选献金项目数据分析

    数据分析06 /pandas高级操作相关案例:人口案例分析.2012美国大选献金项目数据分析 目录 数据分析06 /pandas高级操作相关案例:人口案例分析.2012美国大选献金项目数据分析 1. ...

  2. pandas高级操作

    pandas高级操作 import numpy as np import pandas as pd from pandas import DataFrame,Series 替换操作 替换操作可以同步作 ...

  3. 数据分析05 /pandas的高级操作

    数据分析05 /pandas的高级操作 目录 数据分析05 /pandas的高级操作 1. 替换操作 2. 映射操作 3. 运算工具 4. 映射索引 / 更改之前索引 5. 排序实现的随机抽样/打乱表 ...

  4. Pandas高级教程之:GroupBy用法

    Pandas高级教程之:GroupBy用法 目录 简介 分割数据 多index get_group dropna groups属性 index的层级 group的遍历 聚合操作 通用聚合方法 同时使用 ...

  5. [Session] SessionHelper2---C#关于Session高级操作帮助类 (转载)

    点击下载 SessionHelper2.rar 这个类是关于Session的一些高级操作1.添加时限制时间2.读取对象3.读取数据等等看下面代码吧 /// <summary> /// 联系 ...

  6. cassandra高级操作之索引、排序以及分页

    本次就给大家讲讲cassandra的高级操作:索引.排序和分页:处于性能的考虑,cassandra对这些支持都比较简单,所以我们不能希望cassandra完全适用于我们的逻辑,而是应该将我们的逻辑设计 ...

  7. pandas小记:pandas高级功能

    http://blog.csdn.net/pipisorry/article/details/53486777 pandas高级功能:面板数据.字符串方法.分类.可视化. 面板数据 {pandas数据 ...

  8. MySQL学习笔记_9_MySQL高级操作(上)

    MySQL高级操作(上) 一.MySQL表复制 create table t2 like t1;               #复制表结构,t2可以学习到t1所有的表结构 insert into t2 ...

  9. MySQL学习笔记_10_MySQL高级操作(下)

    MySQL高级操作(下) 五.MySQL预处理语句 1.设置预处理stmt,传递一个数据作为where的判断条件 prepare stmt from "select * from table ...

随机推荐

  1. json-lib使用——JSONObject与JSONArray

    ps:看这篇博客之前首先要引入工具包json-lib-2.2.2-jdk15.jar 资源链接:百度云:链接:https://pan.baidu.com/s/1o9k7PSu 密码:00lj 一.从O ...

  2. golang基础--控制语句

    go基础之控制语句 补充知识 指针 与其他语言不同,在Go中不支持指针运算即->运算符,而直接采用.选择符来操作指针目标对象的成员. 操作符&取变量的地址,使用*通过指针间间接访问目标对 ...

  3. Java中的数据验证

    原文链接:https://www.cuba-platform.com/blog/2018-10-09/945 翻译:CUBA China CUBA-Platform 官网 : https://www. ...

  4. GetHashCode方法学习

    GetHashCode方法我的理解是做两个对象的比较,每个对象,不管是值类型还是应用类型都提供这个基本函数,都可以去重写它.GetHashTable通常用于HashTable.List<> ...

  5. WPF备忘录(7)WPF图片资源路径介绍

    在项目中增加两张图片Content.jpg和Resource.jpg,分别将其生成操作属性设置为Content和Resource.     在界面中增加两个Image控件ImgContent和ImgR ...

  6. winform绑定多张图片

    开发winform程序的时候经常设计到要显示多张图片的问题,其解决思路一般是先遍历文件夹中的所有图片,然后再把这些图片添加到ImageList控件中,最后再绑定显示出来.这里我们介绍两种绑定的方法: ...

  7. NSLayoutConstraint 遍历查找对应的约束

      当我们使用纯代码方式Autolayout进行布局约束时,一个view上可能添加了很多的约束.而这些约束又不像view一样有一个可以区分的tag值,茫茫约束中想查到想要的约束然后进行更改,好像很难. ...

  8. 三:Jquery-event

    一:jq中事件 1.页面载入事件 ready()方法 格式: $(document).ready(function(){}); $(function(){}); 2.绑定事件 click(),dblc ...

  9. HDU 2824 The Euler function --------欧拉模板

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  10. Code Signal_练习题_Sort by Height

    Some people are standing in a row in a park. There are trees between them which cannot be moved. You ...