【问题标题】:Looping in pandas and apply regular expressions在熊猫中循环并应用正则表达式
【发布时间】:2018-08-04 21:23:49
【问题描述】:

我有以下脚本来选择我的数据集中的列:

 df_c = pd.DataFrame(df, columns =['Year','Month','Title','Term','date','Company','Location'])

我将以下代码分别作为查找和替换每个变量应用:

 df_c['Location'] = df_c['Location'].str.replace(',', '')

假设我有 20 个变量,我将如何将代码同时应用于所有变量?循环“?

【问题讨论】:

    标签: python pandas loops


    【解决方案1】:

    我相信您需要replaceregex=True 来替换子字符串:

    df_c = df_c.replace(',', '', regex=True)
    

    如果只想对某些列应用替换:

    cols = ['Year','Month','Title','Term']
    
    df_c[cols] = df_c[cols].replace(',', '', regex=True)
    

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 2015-11-18
      • 2018-09-06
      • 2017-06-18
      • 2014-10-07
      • 2018-02-17
      • 2017-07-07
      • 2019-10-19
      相关资源
      最近更新 更多