Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 合并不同的列值-熊猫_Python_Pandas_Sklearn Pandas - Fatal编程技术网

Python 合并不同的列值-熊猫

Python 合并不同的列值-熊猫,python,pandas,sklearn-pandas,Python,Pandas,Sklearn Pandas,我有九列'instlevel1','instlevel2','instlevel3','instlevel4','instlevel5','instlevel6','instlevel7','instlevel8','instlevel9' 此列上的值填充如下:如果instlevel1值为1,则所有其他列的值为0;如果instlevel2值为1,则所有其他列(包括instlevel1)的所有其他值为0 我想在一列上“旋转”这个。我得到了预期的结果。但我想知道是否有一种最有效的方法来做到这一点。因

我有九列
'instlevel1','instlevel2','instlevel3','instlevel4','instlevel5','instlevel6','instlevel7','instlevel8','instlevel9'

此列上的值填充如下:如果instlevel1值为1,则所有其他列的值为0;如果instlevel2值为1,则所有其他列(包括instlevel1)的所有其他值为0

我想在一列上“旋转”这个。我得到了预期的结果。但我想知道是否有一种最有效的方法来做到这一点。因为这个案子很重复。这是我所做工作的代码

nivelEducacion = test[['instlevel1','instlevel2','instlevel3', 'instlevel4', 'instlevel5','instlevel6','instlevel7','instlevel8','instlevel9']].idxmax(axis=1)

test['nivelEducacion'] = nivelEducacion
test['nivelEducacion'] = test['nivelEducacion'].replace(['instlevel1'], '1')
test['nivelEducacion'] = test['nivelEducacion'].replace(['instlevel2'], '2')
test['nivelEducacion'] = test['nivelEducacion'].replace(['instlevel3'], '3')
test['nivelEducacion'] = test['nivelEducacion'].replace(['instlevel4'], '4')
test['nivelEducacion'] = test['nivelEducacion'].replace(['instlevel5'], '5')
test['nivelEducacion'] = test['nivelEducacion'].replace(['instlevel6'], '6')
test['nivelEducacion'] = test['nivelEducacion'].replace(['instlevel7'], '7')
test['nivelEducacion'] = test['nivelEducacion'].replace(['instlevel8'], '8')
test['nivelEducacion'] = test['nivelEducacion'].replace(['instlevel9'], '9')
test['nivelEducacion'] = test.nivelEducacion.astype('category')
test = test.drop(['instlevel1', 'instlevel2','instlevel3','instlevel4','instlevel5','instlevel6','instlevel7','instlevel8','instlevel9'], axis=1)
您可以从pandas使用的函数。这也许不是最好的解决方案,但它确实起到了作用:

s = pd.Series(list('aaabbbccddefgh')).astype('category') # generate fake dataset
df = pd.get_dummies(s) # fake df like you have (One Hot Encoded)

df2 = pd.melt(df, value_vars=["a", "b", "c", "d", "e", "f", "g", "h"])
df2 = df2[df2.value == 1]  # to keep only existing categories
df2.drop("value", axis=1, inplace=True)
我找到的另一个解决办法是

我希望有帮助

尼古拉斯

x = df.stack()  # in that case you have to restrict only to your columns
df2 = pd.Series(pd.Categorical(x[x!=0].index.get_level_values(1))).to_frame()