【问题标题】:Deleting substrings in a panda dataframe using regular expressions使用正则表达式删除熊猫数据框中的子字符串
【发布时间】:2018-09-24 08:45:21
【问题描述】:

您好,我想在我的数据框中的多行和多列中从 date-and-time:{city:{endIndex:8,startIndex:0,value:Atlantic} 中删除 {endIndex:8,startIndex:0,value:

这是我的数据框中一行的示例。

{ "_id" : ObjectId("5ad13551df70d8025b7811db"), "Article url" : "http://wreg.com/2015/08/02/mpd-officer-killed-in-the-line-of-duty-identified/",  "Full text" : "Blah blah", "Json" : "{circumstances:{number-of-shots-fired:{endIndex:-1,startIndex:-1,value:},type-of-gun:{endIndex:-1,startIndex:-1,value:}},date-and-time:{city:{endIndex:7,startIndex:0,value:MEMPHIS},clock-time:{endIndex:226,startIndex:218,value:9:15 p.m}, victim-section:[{age:{endIndex:151,startIndex:148,value:33,},gender:Male,name:{endIndex:146,startIndex:135,value:Sean Bolton},race:{endIndex:-1,startIndex:-1,value:},victim-was:[killed]}]}", "Publication date" : "2015-08-03 10:24:27+00:00" }

如您所见,{endIndex:,startIndex:,value:} 显示在一堆具有不同值(有时没有值)的不同列中。 那么我该如何从

date-and-time:{city:{endIndex:7,startIndex:0,value:MEMPHIS}

到我想要的输出

date-and-time:{city:MEMPHIS}

*我知道这个值没有意义,所以请忽略它。

到目前为止,我一直在考虑使用 pandas.DataFrame.replace 结合正则表达式来查找符合我的条件的子字符串。

new_df=old_df.replace('abc{endIndex:X,startIndex:Y,value:ABC', '', regex=True)

abc、ABC 和 X、Y 等价的正则表达式是什么?他们可以是任何东西。

【问题讨论】:

    标签: python regex pandas dataframe substring


    【解决方案1】:

    希望对您有所帮助。 :)

    new_df=a.replace('{endIndex:(.+),startIndex:(.+),value:', '', regex=True)
    

    【讨论】:

    • 你能回答我吗,你为什么不赞成我的回答。对我来说效果很好。
    猜你喜欢
    • 2018-08-28
    • 2018-03-31
    • 2019-01-09
    • 2020-10-23
    • 1970-01-01
    • 2018-12-25
    • 2011-05-13
    • 2021-03-13
    • 2019-06-16
    相关资源
    最近更新 更多