根据pandas中的关键字将字符串拆分为两个不同的列?

2024-04-29 19:40:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集组成列“有用的”字符串形式的列“object”。在

Pat_ID   Useful_crit
  1      **inclusive range**:age 35 to 75 - type 2 diabetes **exclusive range**: type 1 diabetes
  2      **inclusive range**:patients aged 21 and above **exclusive range**:patients who are mentally `

每列中的字符串由两个常用词组成,即inclusive range和exclusive range。现在,我想从同一个字符串创建两个列,分别是“inclusive range”和“exclusive range”。所以输出应该是这样的

^{pr2}$

如何在python中实现这一点?在


Tags: 数据字符串idageobjecttyperangeinclusive
1条回答
网友
1楼 · 发布于 2024-04-29 19:40:53

有一个办法

In [2519]: (df.Useful_crit.str.split('(\**inclusive\**:|\**exclusive\**:)')
              .apply(pd.Series)[[2,4]])
Out[2519]:
                                 2                          4
0  age 35 to 75 - type 2 diabetes             type 1 diabetes
1      patients aged 21 and above   patients who are mentally

In [2520]: df.join(df.Useful_crit.str.split('(\**inclusive\**:|\**exclusive\**:)')
                     .apply(pd.Series)[[2,4]]
                     .rename(columns={2: 'inclusive', 4: 'exclusive'}))
Out[2520]:
   Pat_ID                                        Useful_crit  \
0       1  **inclusive**:age 35 to 75 - type 2 diabetes *...
1       2  **inclusive**:patients aged 21 and above **exc...

                         inclusive                  exclusive
0  age 35 to 75 - type 2 diabetes             type 1 diabetes
1      patients aged 21 and above   patients who are mentally

相关问题 更多 >