python - 通过正则表达式更改 pandas 列的内容

标签 python pandas dataframe split

我有一个数据框,其中有一列,如下所示

Other via Other on 17 Jan   2019 
Other via Other on 17 Jan   2019 
Interview via E-mail    on  14  Dec 2018
Rejected via    E-mail  on  15  Jan 2019
Rejected via    E-mail  on  15  Jan 2019
Rejected via    E-mail  on  15  Jan 2019
Rejected via    E-mail  on  15  Jan 2019
Interview via   E-mail  on  14  Jan 2019
Rejected via Website on 12 Jan  2019

是否可以将此列分成两部分,一个是“via”之前的内容,另一个是“on”之后的内容?谢谢!

最佳答案

使用 str.extract

df[['col1', 'col2']] = df.col.str.extract('(.*)\svia.*on\s(.*)', expand = True)

    col1        col2
0   Other       17 Jan 2019
1   Other       17 Jan 2019
2   Interview   14 Dec 2018
3   Rejected    15 Jan 2019
4   Rejected    15 Jan 2019
5   Rejected    15 Jan 2019
6   Rejected    15 Jan 2019
7   Interview   14 Jan 2019
8   Rejected    12 Jan 2019

关于python - 通过正则表达式更改 pandas 列的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54261746/

相关文章:

Python 网页抓取 : Beautiful Soup

python - 比较数据框的多列并将结果存储在新列中

python - 在多索引 pandas DataFrame 中打开 'pretty viewing'

r - sparklyr 更改所有列名 spark dataframe

r - 如何用另一行中的单词替换一行中的部分字符串?

Python:为什么 partition(sep) 比 split(sep, maxsplit=1) 快

python - PySpark:执行联合时列数据类型发生变化

python - pandas DataFrame to_html 中的粗体列

python - np.where() 内部存在 & 的问题

python - 从 pandas DataFrame 创建差异矩阵