简体   繁体   中英

Finding a min value among non-numeric columns in pandas

Consider the following dataframe:

#!/usr/bin/python3.5
import pandas as pd # version 0.23.4
x_df = pd.DataFrame([[1.2, 3.4, 'n', 'a'], [5.6, 'a', 'b', 7.8], 
                    [2.2, 'c', 1.35, 'd'], ['k', 'o', 'b', 'c']], 
                    columns=['A', 'B', 'C', 'D'])

I'm trying to achieve, the min value for each row (like below):

x_df =        A    B     C    D     min
         0  1.2  3.4     n    a    1.2
         1  5.6    a     b  7.8    5.6
         2  2.2    c  1.35    d    1.35
         3    k    o     b    c    nan

I tried to do this using:

x_df['min'] = x_df.apply(lambda x: x.min(numeric_only=True), axis=1)

However this throws an error:

NotImplementedError: ('Series.min does not implement numeric_only.', 'occurred at index 0')

Is there a simple way to achieve this without using for loops or lengthy code?

If this question has already been answered, kindly point me and sorry for duplicate (search didn't get me what I wanted !)

Simple way is first convert to numeric using to_numeric , leave the one can not be converted as NaN , then do min

df.apply(pd.to_numeric,errors='coerce',axis=1).min(1)
Out[96]: 
0    1.20
1    5.60
2    1.35
3     NaN
dtype: float64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM