如何使用python在两列上提取给定条件的行?

时间:2016-06-24 03:24:56

标签: python pandas dataframe conditional extract

os.chdir("C:\Users\EDAWES01\Desktop\Cookie profiling")
data = pandas.read_csv('activity_url.csv', delimiter=';')
data_read=np.array(data)
quantity = data_read[0:, 2]
other_data = data_read[0:, 1]
x="http"
url_data = data_read[np.logical_and(quantity==1, any(x in other_data)][:,1] #extraction

我收到此错误:

  

TypeError:'bool'对象不可迭代

1 个答案:

答案 0 :(得分:1)

您可以使用iloc进行选择,使用boolean indexing str.contains进行选择:

import pandas as pd
import io

temp=u"""0;1;2
0;http;1
5;http1;0
8;aa2;7
9;fffhttp;1"""
#after testing replace io.StringIO(temp) to filename
data = pd.read_csv(io.StringIO(temp), sep=";")
print (data)
   0        1  2
0  0     http  1
1  5    http1  0
2  8      aa2  7
3  9  fffhttp  1

x="http"
url_data = data[(data.iloc[:, 2] == 1) & (data.iloc[:, 1].str.contains(x))]
print (url_data)
   0        1  2
0  0     http  1
3  9  fffhttp  1

print (url_data.iloc[:,1])
0       http
3    fffhttp
Name: 1, dtype: object