首页 > 解决方案 > 在熊猫数据框中创建一组随机列名

问题描述

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

如何生成 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

标签: python-3.xpandasdataframe

解决方案


假设您遇到的问题是缺少 , 的分组,"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

推荐阅读