我正在尝试创建一个csv数据集,其中包含4列“name”“age”“weight”“height”和100行此列的随机数据,但第一步中的代码给了我一行而不是100行,如何修复此问题以及如何在csv文件中转换此问题
`import random
import pandas as pd
import numpy as np
person="person"
personList =[person+str(i) for i in range(100)]
ageList=[random.randint(1,90) for i in range(100)]
weightList=[random.randint(40,150) for i in range(100)]
heightList=[random.randint(140,210) for i in range(100)]
raw_data={'Name':[personList],
'Age':[ageList],
'Weight':[weightList],
'Height':[heightList]}
df = pd.DataFrame([raw_data])
print(df)`
numpy
非常擅长构建随机数组,pandas
在内部使用numpy
数组。所以我的建议是:不要将值作为“列表列表”传入,即删除外部
[ ]
:要输出为csv,请使用:
[输出]
另一种方法是使用
numpy.random
,其中大多数方法都有一个size
参数:相关问题 更多 >
编程相关推荐