转换熊猫数据框以用于MultiLabelBinarizer

时间:2018-11-27 07:41:22

标签: python dataframe scikit-learn transformation multilabel-classification

我的问题是:如何转换这样的数据框以最终在scikit的MulitLabelBinarizer中使用它:

d1 = {'ID':[1,2,3,4], 'km':[80,90,90,100], 'weight':[10,20,20,30], 'label':['A','B','C','D','E']}
df1 = pd.DataFrame(data=d1)
df1

    ID  km  weight label
0   1   80      10     A
1   2   90      20     B
2   2   90      20     C
3   4  100      30     D

它应该像这样弹奏:

d2 ={'km':[80,90,100], 'weight':[10,20,30], 'label':['A',('B','C'),'D']}
df2 = pd.DataFrame(data=d2)
df2

    km  weight   label
0   80      10       A
1   90      20  (B, C)
2  100      30       D

所以我可以在MultiLabelBinarizer中正确使用数据:

from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
mlb.fit(df2['label'])
mlb.transform(df2['label'])

array([[1, 0, 0, 0],
       [0, 1, 1, 0],
       [0, 0, 0, 1]])

注意:原始数据有超过一百万行。

1 个答案:

答案 0 :(得分:0)

我认为您需要这个:

d1 = {'ID':[1,2,3,4], 'km':[80,90,90,100], 'weight':[10,20,20,30], 'label':['A','B','C','D']}
df1 = pd.DataFrame(data=d1)
#Groupby and get tuple, like you need 
df2 = pd.DataFrame(df1.groupby(['km','weight'])['label'].apply(lambda x: tuple(x.values)))
df2.reset_index(inplace=True)

from sklearn.preprocessing import MultiLabelBinarizer
mlb = MultiLabelBinarizer()
mlb.fit(df2['label'])
mlb.transform(df2['label'])