python-3.x - 在熊猫数据框中创建一组随机列名
问题描述
我正在尝试创建一组列(在熊猫数据框中),其中列名是随机的。这是因为我想以随机方式从更大的数据集中生成过滤数据。
如何生成 N (= 4) * 3 组列名,如下所示?
car_speed state_8 state_17 state_19 state_16 wd_8 wd_17 wd_19 wd_16 wu_8 wu_17 wu_19 wu_16
我的潜在代码如下,但并没有真正起作用。我首先需要块'state_',然后是'wd_',然后是'wd_'。我下面的代码按连续顺序分别生成“state_”、“wd_”、“wu_”。当它按这个顺序时,我在从更大的数据集中填充数据时遇到了问题
def iteration1(data, classes = 50, sigNum = 4):
dataNN = pd.DataFrame(index = [0])
dataNN['car_speed'] = np.zeros(1)
while len(dataNN.columns) < sigNum + 1:
state = np.int(np.random.uniform(0, 50))
dataNN['state_'+str(state)] = np.zeros(1) # this is the state value set-up
dataNN['wd_' + str(state)] = np.zeros(1) # this is the weight direction
dataNN['wu_' + str(state)] = np.zeros(1) # this is the weight magnitude
count = 0 # initialize count row as zero
while count < classes :
dataNN.loc[count] = np.zeros(len(dataNN.columns))
for state in dataNN.columns[1:10]:
dataNN[state].loc[count] = data[state].loc[count]
count = count + 1
if count > classes : break
return dataNN
解决方案
假设您遇到的问题是缺少 , 的分组,"state_*"
我建议您首先选择随机整数,然后使用它们来标记列。如下所示:"wd_*"
"wu_*"
sigNum / 3
states = [np.int(np.random.uniform(0, 50)) for _ in range (sigNum/3)]
i = 0
while len(dataNN.columns) <= sigNum:
state = states[i]
i += 1
dataNN['state_'+str(state)] = np.zeros(1) # this is the state value set-up
dataNN['wd_' + str(state)] = np.zeros(1) # this is the weight direction
dataNN['wu_' + str(state)] = np.zeros(1) # this is the weight magnitude
推荐阅读
- ios - 如何移动 tmp uri 中包含 /private/ 的文件
- sql - SQL查询从不同的表返回2个值
- python - 如何在 python 中使用 enumerate() 枚举字典中的项目
- java - 如何使用不同的 url 模式注册几个不同的过滤器?
- tableau-api - 当原始字段使用固定 LOD 计算时,如何计算画面中的百分比变化?
- android - Flutter 项目 - 创建时重复的所有文件
- xamarin - 如何将我的 c# 对象转换为 json 并在领域数据库中保存为字符串?
- javascript - 为什么在浏览器中运行 expo 应用程序时导出默认会引发错误?
- node.js - ag-grid-community 与 ag-grid-enterprise 新网格
- python - 使游戏棒英雄自动化