2013-08-27 133 views
1

我有一个熊猫数据帧,看起来像这样:选择大熊猫行基于列表值

<class 'pandas.core.frame.DataFrame'> 
Int64Index: 1072 entries, 0 to 1071 
Data columns (total 10 columns): 
city   1072 non-null values 
latitude  1072 non-null values 
longitude  1072 non-null values 
manufacturer 1072 non-null values 
name   1072 non-null values 
offering  1072 non-null values 
platformID  1072 non-null values 
procedure  1072 non-null values 
properties  1072 non-null values 
sensorID  1072 non-null values 
dtypes: object(10) 

properties是字符串值的列表:

df_devices.head(1).properties 
Out[79]: 0 [urn:average_wind, urn:dew_point] 

我想选择记录的是只包含'urn:dew_point'条目,但我不知道如何过滤它们(使用isin或替代方法)

回答

3

您可以简单地使用apply来完成S:

In [11]: df = pd.DataFrame([[['urn:dew_point'], 1]], columns=['properties', 'id']) 

In [12]: df 
Out[12]: 
     properties id 
0 [urn:dew_point] 1 

In [13]: df[df['properties'].apply(lambda x: 'urn:dew_point' in x)] 
Out[13]: 
     properties id 
0 [urn:dew_point] 1 

如果这是一个简单的字符串列的一部分,你可以使用str.contains

In [21]: df = pd.DataFrame([['urn:dew_point', 1]], columns=['properties', 'id']) 

In [22]: df['properties'].str.contains('urn:dew_point') 
Out[22]: 
0 True 
Name: properties, dtype: bool 

In [23]: df[df['properties'].str.contains('urn:dew_point')] 
Out[23]: 
     properties id 
0 urn:dew_point 1