itsaruns
itsaruns

Reputation: 669

selecting columns equal to a field in pandas dataframe

My Pandas DataFrame looks like this:

0                   STUN
1                  Webex
2                    PPP
3                MyVideo
4                Icecast
5               PPSTREAM
6                    FTP
7                   SPDY
8     Thunder/Webthunder
9                    IRC
10            CitrixGoTo
11                 FLASH
12               GameKit
13                   RDP
14                IMplus
...
505         unknown
506      BitTorrent
507          ISAKMP
508            HTTP
509       REALMEDIA
510     Silverlight

From this I have to select the columns which are equal to HTTP and SSH.

Upvotes: 1

Views: 4607

Answers (1)

Andy Hayden
Andy Hayden

Reputation: 375475

You can use the isin Series method on a column:

df[df[column_name].isin(['HTTP', 'SSH'])]

An alternative is to check for either being equal (most likely this will be faster):

df[(df[column_name] == 'HTTP') | (df[column_name] == 'SSH'])]

Upvotes: 4

Related Questions