-1

I have a CSV loaded into Pandas that looks like this...

col1  |  col2  |  col3  |  col4
--------------------------------
1     |  red   |  big   |  round
1     |  blue  |  small |  square
1     |  pink  |  small |  triangle
1     |  puple |  big   |  rectangle
1     |  green |  big   |  round

I want to only keep rows where col4 is either round or triangle. I know I can do this...

df[df.col4 == 'round']

But how do I also include triangle in this conditinoal?

But how can I do the opposite and only keep the row if round or traignle?

fightstarr20
  • 11,682
  • 40
  • 154
  • 278

1 Answers1

2

A really good example of this has been provided in another answer: https://stackoverflow.com/a/19960116/6294483

For your specific case:

You should use the pandas.DataFrame.isin() method.

For example in your case:

print(df[df.col4.isin(['round','triangle'])])

Should give you the following output:

   col1   col2   col3      col4
0     1    red    big     round
2     1   pink  small  triangle
4     1  green    big     round

If you wanted opposite you could use boolean vector operations (specifically the ~ operator):

print(df[~df.col4.isin(['round','triangle'])] )

Which should give you the following:

   col1   col2   col3       col4
1     1   blue  small     square
3     1  puple    big  rectangle
chumbaloo
  • 671
  • 6
  • 16
  • Thanks for that example and link, would this still be the appropriate method to use if I wanted to output the result as a seperate CSV instead of print? – fightstarr20 May 14 '20 at 14:51
  • 1
    Yes absolutely. Have a look at the pandas.DataFrame.to_csv() method https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html#pandas-dataframe-to-csv So in your case it would be as simple as df[~df.col4.isin(['round','triangle'])].to_csv('output_file.csv') – chumbaloo May 14 '20 at 14:52