1

Suppose I have this data frame:

pd.DataFrame({"A": [1,'3', "4-Very Good"], "B":['1', '2', '3}'], "C": ['Apple', 'Ball', 'Cat'], "D": [1,4,"5-Excellent"]})

Column “A”: 1) I want both 1 and ‘3’ to be converted to float ; 2) do not want to convert ‘4-Very Good’ to NaN.

Column “B”: I want to make each of “1”, “2”, “3” to float.

Column “C”: should be unchanged.

Column “D”: 1)I want to change “1” and “4” to float; 2)do not want to convert “4-Very Good” to NaN.

0

1 Answer 1

3

One way is casting to float with pd.to_numeric all existing numbers, and fillna back with df:

df.apply(pd.to_numeric, errors='coerce').fillna(df)

             A   B      C            D
0            1   1  Apple            1
1            3   2   Ball            4
2  4-Very Good  3}    Cat  5-Excellent
​

An appreciation from @aLollz: By simply doing df.apply(pd.to_numeric, errors='ignore') here we are not solving the problem. When an entire column cannot be cast to float, it remains unchanged. We must coerce the errors so the column becomes of type float, and then fillna with the string values, ending up with an object dtype column in the case any values are filled, but with the numerical vales remaining as floats.

Sign up to request clarification or add additional context in comments.

15 Comments

You can do this using ignore instead and avoid the coercion and filling after, eg: df.apply(pd.to_numeric, errors='ignore')
No worries - will delete this and my other comment in a mo'.
Ooops... sorry yatu - should have seen that :( Good catch @ALollz!
Guess then df.applymap(lambda v: pd.to_numeric(v, errors='ignore')) - that'll do the same as a fillna after, but whether it's desirable or any better... and there's still the question of why one would want to do this anyway on values... but... guess that's for another day
oh ouch ouch ouch - you're completely right - that's just horrible... so yeah... definitely you were right here and my suggestion was misleading at best - sorry again!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.