Skip to main content
 首页 » 编程设计

python之如何按列值(字符串)过滤 DataFrame 由 const 字符串包含

2025年02月15日20duanxz

我有一个像这样的数据框(数据):

              mac  len      corp                               detail 
18025    14:1F:BA    8  IeeeRegi          IEEE Registration Authority 
18026  14:1F:BA:0   10  Shenzhen  Shenzhen Mining Technology Co.,Ltd. 
18027  14:1F:BA:1   10   Gloquad                                  NaN 
18028  14:1F:BA:2   10  Deutsche      Deutsche Energieversorgung GmbH 
18029  14:1F:BA:3   10   Private                                  NaN 

我想使用 const str 来过滤 data['mac'] 包含在 str 中的数据。 例如,如果 str 为“14:1F:BA:14:E4:5E”,则结果为第 18025 和 18027 行。

我该怎么做?

谢谢。

请您参考如下方法:

试试这个:

# df is your dataframe 
Str = "14:1F:BA:14:E4:5E" 
 
#filter the dataframe by condition: df.mac is contained in Str 
df_filtered = df[df.mac.apply(Str.startswith)] 

如果你想获取索引,你可以使用index:

result = df_filtered.index