问题
I have data
date id request
0 2016-06-17 09:25:05 yans.bouts@yandex.ru GET HTTP/1.1
1 2016-06-17 09:25:07 yans.bouts@yandex.ru POST HTTP/1.1
2 2016-06-17 09:25:47 yans.bouts@yandex.ru CONNECT HTTP/1.1
3 2016-06-17 09:25:47 yans.bouts@yandex.ru POST HTTP/1.1
4 2016-06-17 09:25:49 yans.bouts@yandex.ru CONNECT HTTP/1.1
I need to iterate string and 'GET' not in df['request']
I want to delete string from df.
Desire output
date id request
0 2016-06-17 09:25:05 yans.bouts@yandex.ru GET HTTP/1.1
I try df = df['GET' in df.request]
but it returns
KeyError: False
回答1:
You need boolean indexing with mask
created by str.contains:
print (df.request.str.contains('GET'))
0 2016-06-17 True
1 2016-06-17 False
2 2016-06-17 False
3 2016-06-17 False
4 2016-06-17 False
print (df[df.request.str.contains('GET')])
date id request
0 2016-06-17 09:25:05 yans.bouts@yandex.ru GET HTTP/1.1
EDIT by comment:
For comparing column size
use []
, because size is function:
df_upd = df_upd[df_upd['size'].astype(int) > 3000]
来源:https://stackoverflow.com/questions/37991058/how-to-iterating-string-in-df-using-python