【问题标题】:Delete abbreviations (combination of Letter+dot) from Pandas column从 Pandas 列中删除缩写(字母+点的组合)
【发布时间】:2022-01-17 19:08:36
【问题描述】:

我想删除 pandas 列中字符串的特定部分,例如后跟一个点的任何字母。例如,有一个名称列:

John W. Man
Betty J. Rule
C.S. Stuart

剩下的应该是

John Man
Betty Rule
Stuart

所以,任何字母后跟一个点,代表一个缩写,应该去。 我想不出 str.replace 之类的方法。

【问题讨论】:

    标签: pandas string replace


    【解决方案1】:

    使用Series.str.replace 和reegx 匹配一个字母与. 和它后面的空格(如果存在):

    df['col'] = df['col'].str.replace('([a-zA-Z]{1}\.\s*)','', regex=True)
    print (df)
              col
    0    John Man
    1  Betty Rule
    2      Stuart
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 2015-07-04
      相关资源
      最近更新 更多