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. web前端安全 XSS跨站脚本 CSRF跨站请求伪造 SQL注入

    web安全,从前端做起,总结下web前端安全的几种技术: 1,XSS XSS的全称是Cross Site Scripting,意思是跨站脚本,XSS的原理也就是往HTML中注入脚本,HTML指定了脚本 ...

  2. transfromjs动画效果

    记得以前facebook做过一款HTML5游戏.开场动画是一块软体类似豆腐的东西一起摇摆.类似的效果如下面的gif所示: facebook当时使用的是createjs下的子项目easeljs和twee ...

  3. 关于NIO

    操作系统的IO控制 在整个IO控制方式的发展过程中,始终贯穿着这样一条宗旨:即尽量减少主机对IO控制的干预,把主机从繁杂的IO控制事务中解脱出来,以便更多地去完成数据处理任务.为了缓和高速CPU和IO ...

  4. array_filter,匿名函数

    static function get_categoryII() { return array( array('id' => 1, 'c1id' => 1, 'name' => '1 ...

  5. CSS中定位机制的想法

    对于一个刚刚接触css的新手而言,CSS的定位机制可能是最让人头疼的一件事情了, 接下来我们了解一下CSS的定位机制. position:static | relative | absolute | ...

  6. table边框单线的实现方法

    1.实现方法一:    <table border="0" cellspacing="1" style="    实现原理:利用table的单元 ...

  7. C# 从字符串中取出英文字母

    string fid = context.Request["value2"];//fid=FCT1234 Regex re = new Regex(@"[a-zA-Z]+ ...

  8. june 14

    Thank you for your applying for employment with our company. Your application is now being processed ...

  9. 利用Jurassic在.net下运行js函数

    static void Main(string[] args) { var eng = new Jurassic.ScriptEngine(); eng.Evaluate("function ...

  10. (转)SQL Server 性能调优(cpu)

    摘自:http://www.cnblogs.com/Amaranthus/archive/2012/03/07/2383551.html 研究cpu压力工具 perfom SQL跟踪 性能视图 cpu ...