0

I am transitioning from R to Python right now and have a sample pandas dataframe as follows:

 df = pd.DataFrame({'x': pd.Series([1.0, 'Joe Young', '3M-Fit']), 'y': pd.Series(['1000', '1001', '1002'], dtype=int), 'z' : pd.Series(['Henry', 'Henry','Henry'])})

           x     y      z
0          1  1000  Henry
1  Joe Young  1001  Henry
2     3M-Fit  1002  Henry

When I look at the datatype of each row for first column it is combination of str and float:

    df['x'].map(lambda x: type(x))

0    <type 'float'>
1      <type 'str'>
2      <type 'str'>
Name: x, dtype: object

What I would like to do is to print those rows of dataframe (x and y columns included) where type(x) is a float. So, in this case I would like sample output as:

               x     y      z
0              1    1000   Henry

I looked here and here. But it either applies to complete dataframe or gives True and False values. I want to apply it to each row of a particular column of interest and want to get real values in all columns for those rows of interest.

Community
  • 1
  • 1
Craig Bing
  • 319
  • 3
  • 14

1 Answers1

1

This should do the job for you.

df[df.applymap(np.isreal).all(1)]

It defines the rows that are all real and select only those rows.


with a slight improvement to account automatically for all numeric types, and also account for the edit in the question. Especially for the edit, you can explicitly restrict the test to the columns of interest.

import numbers
df[df[['x','y']].applymap(lambda x: isinstance(x, numbers.Number)).all(1)]
innoSPG
  • 4,588
  • 1
  • 29
  • 42
  • 1
    Thanks...but this would work if ALL columns are numerical for a given row. However, lets say I have another column which is all strings. In that case, this solution would not work. I have updated the sample data frame to take this into account. Any idea how it would work in the case of updated dataframe example? – Craig Bing Mar 01 '16 at 21:30
  • 1
    @CraigBing For that case, you can explicitly restrict the test to the columns of interest; See the edit in the answer. Also pay attention to the improvement that automatically account for all numeric types. – innoSPG Mar 01 '16 at 21:38