我有这样一个数据帧:
text text2 category
sfsd sgvv sfsdfdf abc,xyz
zydf sefs sdfsd drdg yyy
dfsd dsrgd dggr dgd xyz
eter vxg wfe fs abc
dfvf ertet dggdss abc,xyz,bbb
我想要这样的输出:
text text2 category
sfsd sgvv sfsdfdf abc
sfsd sgvv sfsdfdf xyz
zydf sefs sdfsd drdg yyy
dfsd dsrgd dggr dgd xyz
eter vxg wfe fs abc
dfvf ertet dggdss abc
dfvf ertet dggdss xyz
dfvf ertet dggdss bbb
基本上,在category
列中为每两个或更多类别创建一个新行
我试过这个:
df1 = (df.assign(category = df['category'].str.split(','))
.explode('category')
.reset_index(drop=True))
但它似乎创造了比预期更多的行。在我最初的df中,我有很多列,不仅仅是text、text2和category
我的原始数据帧的屏幕截图
这里category
=NER_Category
以下是代码的输出:
您仍然可以使用explode来执行此操作
这应该做到:
相关问题 更多 >
编程相关推荐