【发布时间】:2021-01-20 09:16:35
【问题描述】:
跟进这个答案How to keep only a certain set of rows by index in a pandas DataFrame 我需要扩展一下:
E. G。我只想保留其索引遵循规则的行。在我的情况下,“01”到“99”。我必须写一个从 01 到 99 的系列来过滤行吗?
或者会是某种正则表达式好吗? gooddf.loc['[0-9]']
【问题讨论】:
跟进这个答案How to keep only a certain set of rows by index in a pandas DataFrame 我需要扩展一下:
E. G。我只想保留其索引遵循规则的行。在我的情况下,“01”到“99”。我必须写一个从 01 到 99 的系列来过滤行吗?
或者会是某种正则表达式好吗? gooddf.loc['[0-9]']
【问题讨论】:
在这种情况下,请确保以“^”指定开头,以“$”指定匹配结尾。
import pandas as pd
gooddf = pd.DataFrame({"K":[2,3,4,6,2]}, index=["01", "101", "98", "201", "032"])
match_in = gooddf.index.str.match("^[0-9]{1,2}$")
gooddf[match_in]
【讨论】: