Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas: How can I search for a string in a column? [duplicate]

I have a DataFrame like this:

col1,col2
Sam,NL
Man,NL-USA
ho,CA-CN

And I would like to select the rows whose second column contains the word 'NL', which is something like SQL like command. Does anybody know about a similar command in Python Pandas?

like image 300
UserYmY Avatar asked Oct 24 '25 21:10

UserYmY


1 Answers

The answer is here. Let df be your DataFrame.

df[df['col2'].str.contains('NL')]

This will select all the records that contain 'NL'.

like image 100
Zenadix Avatar answered Oct 26 '25 10:10

Zenadix