在 pandas 中创建发行 [英] Create distribution in Pandas

查看:48
本文介绍了在 pandas 中创建发行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成具有特定分布的随机/模拟数据集.

I want to generate a random/simulated data set with a specific distribution.

作为示例,分布具有以下属性.

As an example the distribution has the following properties.

  1. 1000人口
  2. 性别比例为:男性49%,女性50%,其他1%
  3. 年龄分布如下:0-30(30%),31-60(40%),61-100(30%)

结果数据框将有1000行,两列称为性别和年龄(具有上述值分布)

The resulting data frame would have 1000 rows, and two columns called gender and age (with the above value distributions)

在Pandas或其他图书馆中有没有办法做到这一点?

Is there a way to do this in Pandas or another library?

推荐答案

您可以尝试:

N = 1000
gender = np.random.choice(["male","female", "other"], size=N, p = [.49,.5,.01])

age = np.r_[np.random.choice(range(30),size= int(.3*N)),
       np.random.choice(range(31,60),size= int(.4*N)),
       np.random.choice(range(61,100),size= N - int(.3*N) - int(.4*N) )]
np.random.shuffle(age)

df = pd.DataFrame({"gender":gender,"age":age})

这篇关于在 pandas 中创建发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆