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

How to select rows from pandas dataframe by looking a feature' data types when a feature contains more than one type of value

I have a dataframe with 3 features: id, name and point. I need to select rows that type of ‘point’ value is string.

id name point
0 x 5
1 y 6
2 z ten
3 t nine
4 q two

How can I split the dataframe just looking by type of one feature’ value?

I tried to modify select_dtypes method but I lost. Also I tried to divide dataset with using

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

df[df[point].dtype == str] or df[df[point].dtype is str]

but didn’t work.

>Solution :

Technically, the answer would be:

out = df[df['point'].apply(lambda x: isinstance(x, str))]

But this would also select rows containing a string representation of a number ('5').

If you want to select "strings" as opposed to "numbers" whether those are real numbers or string representations, you could use:

m = pd.to_numeric(df['point'], errors='coerce')
out = df[df['point'].notna() & m]

The question is now, what if you have '1A' or 'AB123' as value?

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