2017-07-16 54 views
2

我有一个数据帧(数据),如:如何过滤通过柱值(字符串)数据帧由一个const字符串cotained

   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过滤数据,其中数据['MAC ']包含在str中。如果str是“14:1F:BA:14:E4:5E”,则结果是行18025和18027.

我该怎么做?

thx。

回答

1

试试这个:

# 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 
+0

THX。有用。 –

+1

'x in'将匹配字符串中的任何位置,不仅在开头。 –

+1

@StephenRauch,固定 –