【问题标题】:Remove values before a particular value in dataframe column using regex using python使用python使用正则表达式删除数据框列中特定值之前的值
【发布时间】:2020-09-09 13:02:07
【问题描述】:

使用 python 使用正则表达式删除数据框列中特定值之前的值 有一个带有值的df:

df1


a         b        
tom      score:abc132
luke     score:123      
mark     score:132
mark     score:132
luke     value:123
tom      value:145
luke     score:123
tom      marrks:145


如何使用正则表达式删除所有 b 列中 : 之前的值。

预期输出:


a         b        
tom      abc132
luke     123      
mark     132
mark     132
luke     123
tom      145
luke     123
tom      145


【问题讨论】:

  • 为什么不使用str.splitdf['b'].str.split(':',expand=True)[1]

标签: python python-3.x regex pandas dataframe


【解决方案1】:

试试这个:

df['b'] = df['b'].str.extract('\:(.*)$')[0]

输出:

      a       b
0   tom  abc132
1  luke     123
2  mark     132
3  mark     132
4  luke     123
5   tom     145
6  luke     123
7   tom     145

【讨论】:

    猜你喜欢
    • 2019-11-10
    • 2021-01-27
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多