3123
3123

Reputation: 121

Convert all numeric values in dataframe to float but leave strings unchanged

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.

Upvotes: 1

Views: 2984

Answers (1)

yatu
yatu

Reputation: 88236

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.

Upvotes: 3

Related Questions