我知道在这个话题上有很多问题,但仍然:
我的输入:作为数据帧
task m_label
0 S101-10061 [Cecum Landmark, ICV, Comment, Appendiceal ori...
1 S101-10069 [Rectum RF, ICV, Cecum Landmark, TI, Comment, ...
2 S101-10078 [Appendiceal orifice, ICV, Cecum Landmark, Com...
3 S101-10088 [Cecum Landmark, ICV, Comment, Appendiceal ori...
4 S101-10100 [Transverse, Appendiceal orifice, ICV, Cecum L...
5 S101-10102 [Rectum RF, ICV, Cecum Landmark, Comment, TI, ...
6 S101-10133 [Rectum RF, Transverse, ICV, Cecum Landmark, C...
7 S101YGBgZ2 [Comment]
我想像df.m_label.str.split("",expand=True)
一样拆分,但它返回NaN
也许df有问题?我从熊猫系列中得到它:m_lab_task=data.groupby(['task'])['m_label'].unique()
。所以,在前面的步骤中,可能是正确的吗
所需输出:
task m_label1 m_label2 m_label3 m_label4 m_label5 m_label6
0 S101-10061 Cecum Landmark ICV Comment Appendiceal orifice
1 S101-10069 Rectum RF ICV Cecum Landmark TI Comment Transverse
2 S101-10078 Appendiceal orifice ICV Cecum Landmark Comment Transverse
Rectum RF
当您将列表转换为数据帧字符串数据时,如果不进行分隔,将合并为单个数据以克服此问题,您必须在转换为数据帧之前插入逗号,如下所示
dataframe应该是这样的
输出代码
使用
str.findall
并传递正则表达式以捕获由单个''
包围的所有内容,然后应用pd.Series
将它们转换为列输出:
如果需要,您可以稍后重置索引,然后
fillna('')
为了给pyguy的答案添加一些内容,如果您想“动态”重命名列,可以使用
add_prefix()
输出:
相关问题 更多 >
编程相关推荐