2

In the following pandas dataframe, In the following, I want the data for ID 1 only and drop rest of the data. How to achieve that?

ID   name  s1       s2       s3       s4

1   Joe   rd       fd       NaN      aa
1   Joe   NaN      hg       kk       NaN
2   Ann   jg       hg       zt       uz
2   Mya   rd       fd       NaN      aa
1   Uri   gg       r        er       rt
4   Ron   hr       t        yt       rt
jaffry_x
  • 169
  • 1
  • 7

2 Answers2

3

All dataframes have a drop method which can do what you want.

For your specific case, if you really want to drop the data instead of filtering it to a different dataframe, then the following snippet:

df.drop(df[df['ID'] != 1].index, inplace = True)

Should work.

Audemed
  • 196
  • 4
2

Use boolean índexing

 df.loc[df['ID'].eq(1)]
ansev
  • 30,322
  • 5
  • 17
  • 31