How to iterating string in df using python

怎甘沉沦 提交于 2020-01-05 05:47:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!