Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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_Data Manipulation - Fatal编程技术网

python-逗号分隔字符串列的二进制编码

python-逗号分隔字符串列的二进制编码,python,pandas,data-manipulation,Python,Pandas,Data Manipulation,有人能帮我对数据进行二进制编码吗?下面的例子如下: df = pd.DataFrame({'_id': [1,2,3], 'test': ['one,two,three', 'one,two', 'two']}) print(df) _id test 0 1 one,two,three 1 2 one,two 2 3 two 到这里: df_result = pd.Dat

有人能帮我对数据进行二进制编码吗?下面的例子如下:

df = pd.DataFrame({'_id': [1,2,3],
                   'test': ['one,two,three', 'one,two', 'two']})

print(df)

   _id           test
0    1  one,two,three
1    2        one,two
2    3            two
到这里:

df_result = pd.DataFrame({'id': [1,2,3],
                          'one': [1,1,0],
                          'two': [1,1,1],
                          'three': [1,0,0]})
print(df_result)

   id  one  three  two
0   1    1      1    1
1   2    1      0    1
2   3    0      0    1
任何帮助都将不胜感激! 谢谢

使用
str.get_dummies()

如果需要,使用
将结果与原始结果合并

In [62]: df.join(df.test.str.get_dummies(','))
Out[62]:
   _id           test  one  three  two
0    1  one,two,three    1      1    1
1    2        one,two    1      0    1
2    3            two    0      0    1
或者,
pd.concat

In [63]: pd.concat([df, df.test.str.get_dummies(',')], axis=1)
Out[63]:
   _id           test  one  three  two
0    1  one,two,three    1      1    1
1    2        one,two    1      0    1
2    3            two    0      0    1
In [63]: pd.concat([df, df.test.str.get_dummies(',')], axis=1)
Out[63]:
   _id           test  one  three  two
0    1  one,two,three    1      1    1
1    2        one,two    1      0    1
2    3            two    0      0    1