如何解决pandas.get_dummies中的问题

时间:2019-01-28 23:06:24

标签: python pandas

我在我的预处理与pd.get_dummies数据集,但结果不是我所需要的。

使用pd.get_dummies()是否正确? 还是我可以尝试的任何方法?

import pandas as pd
rawdataset=[['apple','banana','carrot','daikon','egg'],
           ['apple','banana'],
           ['apple','banana','carrot'],
           ['daikon','egg','fennel'],
           ['apple','banana','daikon']]
dataset=pd.DataFrame(data=rawdataset)
print(pd.get_dummies(dataset))

我希望它看起来像这样:

   apple banana carrot daikon egg fennel 

0   1      1      1     1     1    0
1   1      1      0     0     0    0
........  

不是这样的:

   0_apple  0_daikon  1_banana  1_egg  2_carrot  2_daikon  2_fennel  

0    1         0          1       0       1         0           0
1    1         0          1       0       0         0           0
....

2 个答案:

答案 0 :(得分:1)

用不同的方法给猫皮。


pd.get_dummiesmax

pd.get_dummies(dataset, prefix="", prefix_sep="").max(level=0, axis=1)

   apple  daikon  banana  egg  carrot  fennel
0      1       1       1    1       1       0
1      1       0       1    0       0       0
2      1       0       1    0       1       0
3      0       1       0    1       0       1
4      1       1       1    0       0       0

stackstr.get_dummiessum / max

df.stack().str.get_dummies().sum(level=0)

   apple  banana  carrot  daikon  egg  fennel
0      1       1       1       1    1       0
1      1       1       0       0    0       0
2      1       1       1       0    0       0
3      0       0       0       1    1       1
4      1       1       0       1    0       0

stackcrosstab

u =  df.stack()
pd.crosstab(u.index.get_level_values(0), u)

col_0  apple  banana  carrot  daikon  egg  fennel
row_0                                            
0          1       1       1       1    1       0
1          1       1       0       0    0       0
2          1       1       1       0    0       0
3          0       0       0       1    1       1
4          1       1       0       1    0       0

答案 1 :(得分:0)

您在这里:

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer

rawdataset=[['apple','banana','carrot','daikon','egg'],
            ['apple','banana'],
            ['apple','banana','carrot'],
            ['daikon','egg','fennel'],
            ['apple','banana','daikon']]


def dummy(doc):
    return doc

count_vec = CountVectorizer(tokenizer=dummy, preprocessor=dummy)

count_vec.fit(rawdataset)

X = count_vec.transform(rawdataset).todense()

pd.DataFrame(X, columns=count_vec.get_feature_names())

结果:

   apple  banana  carrot  daikon  egg  fennel
0      1       1       1       1    1       0
1      1       1       0       0    0       0
2      1       1       1       0    0       0
3      0       0       0       1    1       1
4      1       1       0       1    0       0

这里的附加好处是您还可以将其应用于看不见的数据,因为pd.get_dummies不能以相同的方式转换其他看不见的测试数据。

尝试:

unseen_raw_data = [["test"]]
feature_names = count_vec.get_feature_names()
unseen_data = count_vec.transform(unseen_raw_data).todense()
pd.DataFrame(unseen_data, columns=feature_names)

产量:

   apple  banana  carrot  daikon  egg  fennel
0      0       0       0       0    0       0

这是正确的输出