df = pd.DataFrame(np.random.randint(0,6,size=(1200000, 3)),
columns=list('ABC'))
df['sum'] = df[['A','B','C']].sum(axis=1)
df = df[df['sum']==5]
df = df.sample(n=100000)
我想创建一个数据帧,其中包含三个不同的列,其随机数在0到5之间,因此列的总和为5。
答案 0 :(得分:2)
您可以使用itertools排列来查找0到5之间的数字,总和等于5,并将结果分配给DataFrame
import itertools
df = pd.DataFrame([elem for elem in list(itertools.permutations(range(6), 3)) if sum(elem) == 5], columns = list('ABC'))
df['sum'] = df.sum(1)
A B C sum
0 0 1 4 5
1 0 2 3 5
2 0 3 2 5
3 0 4 1 5
4 1 0 4 5
5 1 4 0 5
6 2 0 3 5
7 2 3 0 5
8 3 0 2 5
9 3 2 0 5
10 4 0 1 5
11 4 1 0 5
答案 1 :(得分:0)
module.exports = {
entry: "./main.js", //relative to root of the application
output: {
filename: "./app.bundle.js" //relative to root of the application
},
watchOptions: {
aggregateTimeout: 300,
poll: 1000
}
}
完成数据框
np.random.multinomial(5, [1/3.]*3, size=5) # here when you input size 5 , it only creat 5 lines
Out[38]:
array([[2, 2, 1],
[1, 2, 2],
[0, 3, 2],
[1, 1, 3],
[3, 1, 1]])