繁体   English   中英

在pandas数据框中创建一组随机列名称

[英]create set of randomized column names in pandas dataframe

我正在尝试创建一组列(在panda数据框内),其中列名称是随机的。 这是因为我想以随机方式从较大的数据集生成过滤器数据。

如何按以下方式生成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
import random
import pandas as pd

def iteration1(data, classes = 5, subNum = 15):
    dataNN = pd.DataFrame(index = [0])
    dataNN['car_speed'] = np.zeros(1)

    states = random.sample(range(50), sub_sig)
    for i in range(0, sub_sig, 1):
        dataNN['state_'+str(states[i])] = np.zeros(1) # this is the state value set-up
    for i in range(0, subNum, 1):
        dataNN['wd_' + str(states[i])] = np.zeros(1) # this is the weight direction
    for i in range(0, subNum, 1):
        dataNN['wu_' + str(states[i])] = np.zeros(1) # this is the weight magnitude

    return dataNN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM