Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Specific values in R and Python

I worked with R very smoothly but now I want to work with Python. I have numerical values and into a data frame and now I want to filter specific values. I tried to find solution with other examples but unfortunately, I did find a useful example. Much of example are with strings not with specific numbers.
So first I want to show an example from R, these lines of code work very well.

  filtered_data<-filtered_data<-%>%
              dplyr::filter(items %in% c("2","3","4","5","6","7","14","20","19","23"))

Now I want to replicate these lines of code into Python and I try to do this

filtered_data<-source_data['items']==2,3,4,5,6,7,14,20,19,23]

So can anybody help me with how to filter these specific values in Python?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use Pandas and isin:

filtered_data = source_data[source_data['items'].isin([2,3,4,5,6,7,14,20,19,23])]
print(filtered_data)

# Output
   items
1     19
3      3
4     20
9      5

Setup a MRE:

import pandas as pd
import numpy as np

source_data = pd.DataFrame({'items': np.random.randint(1, 25, 10)})
print(source_data)

# Output
   items
0     12
1     19
2     22
3      3
4     20
5     21
6     10
7     22
8     11
9      5
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading