0

Is there any way that I can drop the value if its index = column index.

I mean, this is my toy dataframe

d = {'Non': [1, 2,4,5,2,7], 'Schzerando': [3, 4,8,4,7,7], 'cc': [1,2,0.75,0.25,0.3,1]}
df = pd.DataFrame(data=d)
df

Then I just want to keep the row which df["cc"] == 1 and 2, like this enter image description here

Toy dataframe to try.

Megan
  • 541
  • 1
  • 3
  • 14
  • 1
    Anyay, yes you want to filter rows where `df['cc'] in [1,2]`. Note: you can do this exactly with integer values, but using `in` on floats can give errors due to floating-point inexactness. – smci Jul 17 '22 at 06:21
  • Oh, got it! So is it the filter function to drop the value? – Megan Jul 17 '22 at 06:25
  • Duplicate of [Filter dataframe rows if value in column is in a set list of values](https://stackoverflow.com/questions/12065885/filter-dataframe-rows-if-value-in-column-is-in-a-set-list-of-values) -> [Use a list of values to select rows from a Pandas dataframe](https://stackoverflow.com/questions/12096252/use-a-list-of-values-to-select-rows-from-a-pandas-dataframe) – smci Jul 17 '22 at 06:26
  • @smci Oh, sorry. I didn't notice!! Thanks for telling me. – Megan Jul 17 '22 at 06:28
  • No, this operation is indeed called 'filtering', but ignore the actual [`filter()` function](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.filter.html), it only filters by label, not by value; yes the name is historically confusing. Here, the [`.isin()` method](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.isin.html) is what you want. – smci Jul 17 '22 at 06:29
  • Yes. Poddar's answer is right for me – Megan Jul 17 '22 at 07:06

2 Answers2

1

You can filter out the rows by converting the cc column to int type then filter by applying mask.

df['cc'] = df['cc'].astype('Int64')
df = df[df['cc'] == 1 | df['cc'] == 2 | df['cc'] == 3]

or you can declare a list with all the values you want to filter for then use pandas isin

f_list = [1,2,3]
df[df['cc'].isin(f_list)]
Himanshu Poddar
  • 7,112
  • 10
  • 47
  • 93
  • Thanks, How to filter 2 or more. ```df = df[df['cc'] == 1 and 2 and 3] ```something like this LOLL? – Megan Jul 17 '22 at 06:27
  • @smci you don't have to worry about it if all you have are integers, that was added to convert string to int type just in case – Himanshu Poddar Jul 17 '22 at 22:05
  • @HimanshuPoddar: yes but if the input contains floats, this will unwantedly truncate them to the integer-floor value, then mismatch them against the ints-of-interest. I'm pointing out the danger. – smci Jul 17 '22 at 22:11
  • That's right, what do you suggest we should type cast to here? – Himanshu Poddar Jul 17 '22 at 22:12
  • Himanshu, it would be fundamentally dodgy programming to have mixed types (int and float) within a single column, yes we could write code that relies on that, but it would be very brittle. – smci Jul 22 '22 at 18:04
  • wdym, I didn't get you? – Himanshu Poddar Jul 22 '22 at 18:08
0
df[(df['cc'] == 1) | (df['cc'] == 2)]

Output :

enter image description here

ASLAN
  • 629
  • 1
  • 7
  • 20