jenryb
jenryb

Reputation: 2117

How to filter pandas dataframe by string

I have a dataframe with several columns. Here is an example:

A          B  C  D      MachineCall    F
0  1 2013-01-02  1  3   Machine1  foo
1  1 2013-01-02  1  3  Machine2  foo
2  1 2013-01-02  1  3   Machine3  foo
3  1 2013-01-02  1  3  Machine1  foo

I want to make a new dataframe that only uses data from rows with Machine1. I don't want to change the order of the columns.

I tried

df2 = df2[(df2 == 'Machine1')]

which got the error

TypeError: Could not compare ['Machine1'] with block values:

and

df2 = df2[(df2'MachineCall'].isin('Machine1')]

which got

TypeError: only list-like objects are allowed to be passed to Series.isin(), you passed a 'str'

Upvotes: 0

Views: 4642

Answers (2)

jenryb
jenryb

Reputation: 2117

The following code works.

df[df['MachineCall'].str.contains("Machine1")] 

Upvotes: 4

DeepSpace
DeepSpace

Reputation: 81594

new_df = df2[df2['MachineCall'] == 'Machine1']

Upvotes: 1

Related Questions