14 抽样
 df.sample(10, replace = True)
df.sample(3)
df.sample(frac = 0.5) # 按比例抽样
df.sample(frac = 10, replace = True,weights = np.random.randint(1,10,6)) # 对样本加权
df.sample(3, axis = 1) # 变量抽样
15 join(即 merge)

 pd.merge(df.sample(4), df.sample(4), how = "left", on = "A", indicator = True)
16 随机数

numpy.random.rand(3, 2) # 按维度生成[0,1)之间的均匀分布随机数
np.random.randn(2,5) # 按维度生成标准正太分布随机数
np.random.randint(2, size=10) # randint(low[, high, size])生成随机整数,默认low为0,high必填,size默认为1
np.random.bytes(10) # 返回随机字节
a=np.arange(10)
np.random.shuffle(a) # 洗牌
a=np.arange(9).reshape(3, 3)
np.random.shuffle(a) # 若是数组,则只会打乱第一维
np.random.permutation(10) # 随机排列,对于多维序列也适用
np.random.permutation(10) .reshape(2, 5)
np.random.seed(1000) # 种子
np.random.normal(2,3,[5,2]) # 高斯分布,其他分布可查
# http://docs.scipy.org/doc/numpy-1.10.1/reference/routines.random.html
np.random.seed(12345678)
x = scipy.stats.norm.rvs(loc=5, scale=3, size=100) # 另外scipy也有这些随机数的生成,附带检验
scipy.stats.shapiro(x)
# http://docs.scipy.org/doc/scipy-0.17.0/reference/stats.html
17 gather和spread

 # gather:
def gather( df, key, value, cols ):
id_vars = [ col for col in df.columns if col not in cols ]
id_values = cols
var_name = key
value_name = value
return pandas.melt( df, id_vars, id_values, var_name, value_name )
# 以上是定义的一个函数,实际上一样的,横变竖,是gather,竖变横,是spread
pd.melt(df, id_vars=['E','F'], value_vars=['A','C'])
# spread:
pd.pivot(df["D"],df["E"],df['F']) #这个是竖变横
df3=pd.pivot(df2['D'],df2['variable'],df2['value'])
df3.reset_index(level=0, inplace=True) # 再变回df的样子
18 熵

 scipy.stats.entropy(np.arange(10)) 

19 字符串拼接

 [",".join(['a','b','d'])]
df[['E','F']].groupby('F')['E'].apply(lambda x: "{%s}" % ', '.join(x)) # 分组拼接,前提是这些列都要是字符串
df[['E','F']].applymap(str).groupby('E')['F'].apply(lambda x: "%s" % ', '.join(x)) # 所以可以这样
20 随机字符串生成

 import random,string
df2 = pd.DataFrame(range(10),columns=['y'])
df2["x"] = [",".join(random.sample(string.lowercase,random.randint(2,5))) for i in range(10)]
21 分列后生成hash表

 # 用20 的示例数据
df3=pd.DataFrame(df2.x.str.split(',').tolist(),index=df2.y).stack().reset_index(level=0)
df3.columns=["y","x"]
22 去重
 df[["F","E"]].drop_duplicates()
23 离散化
 pd.cut(df.A,range(-1,2,1))
 

Python基于pandas的数据处理(二)的更多相关文章

  1. Python基于pandas的数据处理(一)

    import pandas as pd, numpy as np dates = pd.date_range(', periods=6) df = pd.DataFrame(np.random.ran ...

  2. python – 基于pandas中的列中的值从DataFrame中选择行

    如何从基于pandas中某些列的值的DataFrame中选择行?在SQL中我将使用: select * from table where colume_name = some_value. 我试图看看 ...

  3. python使用pandas进行数据处理

    pandas数据处理 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器输入网址http://loc ...

  4. 【python】pandas & matplotlib 数据处理 绘制曲面图

    Python matplotlib模块,是扩展的MATLAB的一个绘图工具库,它可以绘制各种图形 建议安装 Anaconda后使用 ,集成了很多第三库,基本满足大家的需求,下载地址,对应选择pytho ...

  5. 基于pandas python的美团某商家的评论销售数据分析(可视化)

    基于pandas python的美团某商家的评论销售数据分析 第一篇 数据初步的统计 本文是该可视化系列的第二篇 第三篇 数据中的评论数据用于自然语言处理 导入相关库 from pyecharts i ...

  6. 基于 Python 和 Pandas 的数据分析(2) --- Pandas 基础

    在这个用 Python 和 Pandas 实现数据分析的教程中, 我们将明确一些 Pandas 基础知识. 加载到 Pandas Dataframe 的数据形式可以很多, 但是通常需要能形成行和列的数 ...

  7. 基于 Python 和 Pandas 的数据分析(1)

    基于 Python 和 Pandas 的数据分析(1) Pandas 是 Python 的一个模块(module), 我们将用 Python 完成接下来的数据分析的学习. Pandas 模块是一个高性 ...

  8. Python:pandas(二)——pandas函数

    Python:pandas(一) 这一章翻译总结自:pandas官方文档--General functions 空值:pd.NaT.np.nan //判断是否为空 if a is np.nan: .. ...

  9. 基于 Python 和 Pandas 的数据分析(4) --- 建立数据集

    这一节我想对使用 Python 和 Pandas 的数据分析做一些扩展. 假设我们是亿万富翁, 我们会想要多元化地进行投资, 比如股票, 分红, 金融市场等, 那么现在我们要聚焦房地产市场, 做一些这 ...

随机推荐

  1. [知识整理]Java集合(一) - List

    一.实现List的几个类: ArrayList.LinkedList.CopyOnWriteArrayList.Vector 二.几个List底层的数据结构: ArrayList - 数组列表 Lin ...

  2. 介绍开源的.net通信框架NetworkComms框架 源码分析

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 售价249英镑 我曾经花了 ...

  3. javaScript 查询字符串参数 获取

    function getQueryStringArgs() { //取得查询字符串并去掉开头的问号 var qs = (location.search.length > 0 ? location ...

  4. Windows 程序设计

    一.Win32 API /******************************************************************** created: 2014/04/1 ...

  5. Oracle重启 error: ora-01034:oracle not available ora-27101:shared memory realm does not exist

    error: ora-01034:oracle not available ora-27101:shared memory realm does not exist 苦咖啡 他的博客中一篇文章完美的解 ...

  6. WebMethod在webservice里面非静态方法能调用,在页面类里面,静态方法才能调用

    WebMethod在webservice里面非静态方法能调用,在页面类里面,静态方法才能调用

  7. 2.jenkins配置邮件提醒

    1.前言 在Jenkins的使用中邮件提醒是一个常用功能,Jenkins默认安装了Mailer Plugin插件用于实现此功能. 2.邮件服务器配置 首先在Jenkins的"系统管理&quo ...

  8. liunx打开指定端口

    1.切换为root用户 2.切换路径至:/etc/sysconfig 3.vi编辑添加一行: -A INPUT -m state --state NEW -m tcp -p tcp --dport 3 ...

  9. Oracle如何导入导出数据(转自)

    导出:exp ssht/taxware@sshtfile=d:\ssht.dmpexp 用户名/密码@服务名导入:imp ssht/taxware@mysshtfile=d:\ssht.dmp fro ...

  10. SQL 存储过程优化经验

    经现场同事反映,他们用的好好的XML 导出工具最近一直报错,经常报数据库连接超时,查看数据库发现已经有100G 以上有空间了. 但导出数据的存储过程里面每次按时间只导1000多条数据,近理说有时间过滤 ...