Nan Lin
Nan Lin

Reputation: 153

How should I convert a Dataframe full of decimals to floats?

The main problem I have right now is that DataFrame.quantile() doesn't work with Decimals.

I can convert my Dataframe to floats by using df.convert_objects(convert_numeric=True), but this generates a deprecation warning and the suggested replacement infer_objects() doesn't work.

As an alternative, I could use Dataframe.round on my input data instead of converting them to Decimals. I assume that this should be safe for direct comparisons (I am indexing on a column to subtract two dataframes from each other, e.g. df1.set_index('Time') - df2.set_index('Time')) but I can't be certain.

Upvotes: 7

Views: 18262

Answers (3)

jpp
jpp

Reputation: 164673

You can apply pd.to_numeric to an entire dataframe:

df = df.apply(pd.to_numeric, downcast='float')

Upvotes: 14

Marcus V.
Marcus V.

Reputation: 6859

Or try this:

for c in df:
    df[c] = pd.to_numeric(df[c])

Upvotes: -1

TrinTragula
TrinTragula

Reputation: 380

Try doing this:

df.column_name = df.column_name.astype(float)

For reference see this

Upvotes: 8

Related Questions