Python 将带分隔符(';&';)的字符串的单列转换为基于字符串值的带二进制值的多列
我有一百万条带列的数据帧记录,其中包含多个组合字符串,以分隔符作为分隔符 在所需的数据框中,我需要保留该列,并让多个列承载分隔的字符串作为列标题,并基于行中可用的组合使用二进制值 这需要与其他特征相结合,为模型估计器提供信息 随函附上数据样本供参考Python 将带分隔符(';&';)的字符串的单列转换为基于字符串值的带二进制值的多列,python,pandas,string,dataframe,multiple-columns,Python,Pandas,String,Dataframe,Multiple Columns,我有一百万条带列的数据帧记录,其中包含多个组合字符串,以分隔符作为分隔符 在所需的数据框中,我需要保留该列,并让多个列承载分隔的字符串作为列标题,并基于行中可用的组合使用二进制值 这需要与其他特征相结合,为模型估计器提供信息 随函附上数据样本供参考 x.head(20) Genres 793754 Drama|Sci-Fi 974374 Drama|Romance 950027 Horror|Sci-Fi 998553 Comedy 757593 Action|Thriller 943
x.head(20)
Genres
793754 Drama|Sci-Fi
974374 Drama|Romance
950027 Horror|Sci-Fi
998553 Comedy
757593 Action|Thriller
943002 Comedy|Romance
699895 Drama|Romance
228740 Animation|Comedy|Thriller
365470 Comedy
174365 Comedy|Fantasy
827401 Drama
75922 Comedy|Drama
934548 Animation|Children's|Comedy|Musical|Romance
281451 Comedy|Sci-Fi
694344 Sci-Fi
731063 Action|Adventure
978029 Animation|Comedy
283943 Drama|Sci-Fi|Thriller
961082 Action|Adventure|Fantasy|Sci-Fi
778922 Action|Crime|Romance
所需的列(18个)通过独特的功能从整个数据中提取为列表,并根据行字符串数据填充二进制0或1
genre_movies=list(genre_movies.stack().unique())
genre_movies
['Drama',
'Animation',
"Children's",
'Musical',
'Romance',
'Comedy',
'Action',
'Adventure',
'Fantasy',
'Sci-Fi',
'War',
'Thriller',
'Crime',
'Mystery',
'Western',
'Horror',
'Film-Noir',
'Documentary']
我是熊猫的新手,非常感谢您的帮助。请检查这是否是您想要的: (我必须手动输入类型,所以我只在那里放了3行) 代码是:
import pandas as pd
df = pd.DataFrame( {
'Genres' : ['Drama|Sci-Fi', 'Drama|Romance' , 'Horror|Sci-Fi']
},
index = [793754, 974374, 950027] ,
)
genre_movies=list(df.Genres.unique())
genre_movies2 = [words for segments in genre_movies for words in segments.split('|')]
# get a list of unique genres
for genre in genre_movies2:
df[genre] = df.Genres.str.contains(genre, regex=False)
@Ins_hunter建议的方法2
使用.get_dummies()
方法
df2 = df.Genres.str.get_dummies(sep='|')
Action Adventure Animation Children's Comedy Crime Documentary Drama Fantasy Film-Noir Horror Musical Mystery Romance Sci-Fi Thriller War Western
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
1 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0
3 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
4 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1000204 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
1000205 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0
1000206 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0
1000207 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
1000208 0 0 0 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0
1000209 rows × 18 columns
它可以合并回原始数据
df3 = pd.concat([df, df2], axis=1)
@谢谢你的回复。是的,我需要同样的方式,但作为1和0,我们总共有18个这样的功能(所以18或17列)与二进制值。完整的数据集有一百万个观察值。@fenixano-我尝试了我现有的生成的唯一值列表-流派电影。代码如下。对于类型电影中的类型:x['Genres']=x['Genres'].str.contains(genre,regex=False),但出现如下错误。“AttributeError:只能使用带字符串值的.str访问器!”。感谢您的指导,请总结……感谢您的回复。是的,我需要以同样的方式,但作为1和0(所以18或17列)的二进制值。尝试使用我现有生成的唯一值列表-流派电影。代码如下。对于类型电影中的类型:x['Genres']=x['Genres'].str.contains(genre,regex=False),但出现如下错误。“AttributeError:只能使用带字符串值的.str访问器!”。请感谢您的指导,得出结论…@Ins\u hunter 1,True等于1,False等于0,您可以用同样的方法求和。如果您特别需要int 1和0,可以使用
.astype(int)
2,您没有提供数据,因此我无法获得18列,我手动输入4作为演示。3,您得到了错误,因为代码在for循环中,所以您不能单独运行它。如果您至少可以提供我可以导入python的数据帧片段,那么我可以直接修改代码,以便您可以运行它。否则您需要自己修改它。我不知道你的数据帧是被命名为“x”还是“电影类型”之类的else@fenixano-感谢您的回复。已更新数据框的详细信息。在粘贴过程中,结构发生了变形,对此深表歉意。试图运行KNN,但当我运行y_pred=KNN.predict(X_test)语句时,系统被挂起
df3 = pd.concat([df, df2], axis=1)