这些是我要更改为一列的类别。每个列表中的值是数据框中存在的当前二进制列。
housesitu = ['tipovivi1', 'tipovivi2', 'tipovivi3', 'tipovivi4', 'tipovivi5']
educlevels = ['instlevel1', 'instlevel2', 'instlevel3', 'instlevel4', 'instlevel5', 'instlevel6', 'instlevel7',
'instlevel8', 'instlevel9']
regions = ['lugar1', 'lugar2', 'lugar3', 'lugar4', 'lugar5', 'lugar6']
relations = ['parentesco1', 'parentesco2', 'parentesco3', 'parentesco4', 'parentesco5', 'parentesco6',
'parentesco7', 'parentesco8', 'parentesco9', 'parentesco10', 'parentesco11', 'parentesco12']
我目前有以下代码将二进制列合并为分类列:
train['housesitu'] = train[housesitu].idxmax(axis=1)
train.drop(train[housesitu], axis=1, inplace=True)
train['educlevels'] = train[educlevels].idxmax(axis=1)
train.drop(train[educlevels], axis=1, inplace=True)
train['regions'] = train[regions].idxmax(axis=1)
train.drop(train[regions], axis=1, inplace=True)
train['relations'] = train[relations].idxmax(axis=1)
train.drop(train[relations], axis=1, inplace=True)
train['marital'] = train[marital].idxmax(axis=1)
train.drop(train[marital], axis=1, inplace=True)
train['rubbish'] = train[rubbish].idxmax(axis=1)
train.drop(train[rubbish], axis=1, inplace=True)
train['energy'] = train[energy].idxmax(axis=1)
train.drop(train[energy], axis=1, inplace=True)
train['toilets'] = train[toilets].idxmax(axis=1)
train.drop(train[toilets], axis=1, inplace=True)
train['floormat'] = train[floormat].idxmax(axis=1)
train.drop(train[floormat], axis=1, inplace=True)
train['roofmat'] = train[roofmat].idxmax(axis=1)
train.drop(train[roofmat], axis=1, inplace=True)
train['wallmat'] = train[wallmat].idxmax(axis=1)
train.drop(train[wallmat], axis=1, inplace=True)
train['floorqual'] = train[floorqual].idxmax(axis=1)
train.drop(train[floorqual], axis=1, inplace=True)
train['wallqual'] = train[wallqual].idxmax(axis=1)
train.drop(train[wallqual], axis=1, inplace=True)
train['roofqual'] = train[roofqual].idxmax(axis=1)
train.drop(train[roofqual], axis=1, inplace=True)
train['waterprov'] = train[waterprov].idxmax(axis=1)
train.drop(train[waterprov], axis=1, inplace=True)
train['electric'] = train[electric].idxmax(axis=1)
train.drop(train[electric], axis=1, inplace=True)
我想知道是否有更短的方法来做到这一点。
最佳答案
我只能考虑使用groupby
的idxmax
,因为您将列命名为XXXddd
df.groupby(df.columns.to_series().str.replace('\d+',''),axis=1).idxmax(1)
Out[1100]:
A B
0 A2 B2
1 A1 B1
2 A1 B1
数据输入
df=pd.DataFrame({'A1':[1,2,3],'A2':[2,1,3],'B1':[1,2,3],'B2':[2,1,3]})
关于python - 将所有这些二进制列合并为分类列的更简单方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51851684/