【问题标题】:Pandas DF : how to replace '-' by '_' with separators (; CSV format)Pandas DF:如何用分隔符(;CSV 格式)将 '-' 替换为 '_'
【发布时间】:2019-08-02 02:07:59
【问题描述】:

我需要正则表达式和正则表达式函数方面的帮助!!!我有一个用“;”分隔的 CSV 文件并且需要将 - 替换为 _。数据如下所示:

79             80;0;RueSaint_Hilaire;Locale;15-25;1;1             
80              81;0;RueSaint_Hilaire;Locale;5-10;5;5             
81                   82;0;RueTaillon;Locale;10-15;1;1             
82                   83;0;RueTanguay;Locale;10-15;2;2             
83                   84;0;RueTanguay;Locale;15-25;2;2             
84                    85;0;RueTanguay;Locale;5-10;3;3  

例如,我需要将 15-25 替换为 15_25。

到目前为止,我已经尝试过了:

df.replace('-','_', inplace=True)

或者这个:

df_obj = df.select_dtypes(['object'])
df[df_obj.columns] = df_obj.apply(lambda x: x.str.strip())
df.replace('-','_', inplace=True)
print(df)

没有任何成功。这里有任何正则表达式或替换向导可以解决这个小问题吗?

非常感谢!

【问题讨论】:

  • RueSaint-Hilaire 应该更改为RueSaint_Hilaire?您还希望将这些字段读入单独的列,还是您想要一个大字符串?
  • 对不起,我的错误。名称已从以前的数据库中更正。我需要将 15-25、5-10、10-15(等等...)更改为 15_25,5_10,10_15

标签: python regex pandas csv


【解决方案1】:

这是我能想到的最简单的实现:

with open(<PATH TO FILE>, 'r') as fileIn:
    data = fileIn.read()
    print("\nOriginal data: \n", data)
    data = data.replace('-', '_')
    print("Modified data: \n", data)

这给出了:

Original data:
80,0,RueSaint-Hilaire,Locale,15-25,1,1
81,0,RueSaint-Hilaire,Locale,10-May,5,5

Modified data:
80,0,RueSaint_Hilaire,Locale,15_25,1,1
81,0,RueSaint_Hilaire,Locale,10_May,5,5

【讨论】:

    【解决方案2】:

    如果您需要在数字之间专门更改您的-,请选择:

    import re
    
    regex = r"(\d+)-(\d+)"
    
    test_str = ("79             80;0;RueSaint_Hilaire;Locale;15-25;1;1         \n"
        "80              81;0;RueSaint_Hilaire;Locale;5-10;5;5         \n"
        "81                   82;0;RueTaillon;Locale;10-15;1;1         \n"
        "82                   83;0;RueTanguay;Locale;10-15;2;2         \n"
        "83                   84;0;RueTanguay;Locale;15-25;2;2         \n"
        "84                    85;0;RueTanguay;Locale;5-10;3;3  ")
    
    subst = "$1_$2"
    
    # You can manually specify the number of replacements by changing the 4th argument
    result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
    
    if result:
        print (result)
    
    

    您可以在此处查看正则表达式:https://regex101.com/r/DGrm7V/1

    【讨论】:

      【解决方案3】:

      默认为regex=False。因此,使用您现有的代码使用正则表达式替换为regex=Trueinplace=True。见replace

      df.replace('-', '_',regex=True, inplace=True)
      print(df)
      

      【讨论】:

      • 我只是错过了 regex=True 参数。感谢您指出这一点!
      • 是的,这就是为什么我也为您添加了 replace 手册。希望这会有所帮助
      【解决方案4】:

      这里有一个关于 pandas 的很好的常见问题解答:https://stackoverflow.com/tags/pandas/info

      将 lambda 应用于数据框,如下所示:

      df['foo'] = df['foo'].apply(lambda x: x.replace('_', '-'))

      【讨论】:

        【解决方案5】:

        通常,我会选择:

        df['Col'] = df['Col'].str.replace('-', '_')
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-22
          • 1970-01-01
          • 2021-06-14
          • 2010-11-24
          • 1970-01-01
          • 2021-05-22
          • 2016-04-03
          • 2020-09-01
          相关资源
          最近更新 更多