【问题标题】:Extract dataframe with regex使用正则表达式提取数据框
【发布时间】:2020-03-16 20:01:43
【问题描述】:

这是一个从数据帧中获取所有 2xx response_code 的代码,但它告诉我将“提取”改为错误...

数据框是这样的

date_time   response_code
 2/3/10         202
 2/6/10         200
 2/3/12         300
 2/3/11         202

代码:

df_2xx = df_light.response_code.astype(str).str.contains('(2[0-9][0-9])')

我需要

date_time   response_code
 2/3/10         202
 2/6/10         200
 2/3/11         202

谢谢你!

【问题讨论】:

标签: regex python-3.x pandas


【解决方案1】:

使用布尔索引:

df_light[df_light['response_code'].astype(str).str.contains('2[0-9][0-9]')]

输出:

  date_time  response_code
0    2/3/10            202
1    2/6/10            200
3    2/3/11            202

或:

df_light[df_light['response_code'].astype(str).str.startswith('2')]

或:

df_light[df_light['response_code'] % 200 < 100]

【讨论】:

    猜你喜欢
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 2015-02-24
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    相关资源
    最近更新 更多