0

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'

3
  • Shouldn't your title read 'filter' rather than 'sort'? Commented Jul 6, 2015 at 20:31
  • Yes, I can change that. Not sure if this question will still be helpful to others, though. Commented Jul 6, 2015 at 20:34
  • 1
    Regarding your errors the first one is probably because you're trying to compare a df containing mixed dtype columns with a str but this will barf when the dtypes don't match, for the second error it's pretty clear, it should be a list with a single element: df2 = df2[(df2'MachineCall'].isin(['Machine1'])] Commented Jul 6, 2015 at 20:35

2 Answers 2

4

The following code works.

df[df['MachineCall'].str.contains("Machine1")] 
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.