0

I am dealing with some pandas dataframes where all elements are of type Float64Index. Everything I try to convert those elements to a simple float fail.

I have tried pandas.to_numeric and astype(float).

Here is a reproducible dataframe that can be recreated from the following dict

{0: {44: 492.0, 45: 492.0},
 1: {44: Float64Index([506.76], dtype='float64'),
  45: Float64Index([506.76], dtype='float64')},
 2: {44: Float64Index([516.8952], dtype='float64'),
  45: Float64Index([516.8952], dtype='float64')},
 3: {44: Float64Index([527.233104], dtype='float64'),
  45: Float64Index([527.233104], dtype='float64')},
 4: {44: Float64Index([537.77776608], dtype='float64'),
  45: Float64Index([537.77776608], dtype='float64')}}

I would like to have dtypes return float for all the columns.

1
  • This seems like an XY problem where the real issue that should be solved is constructing the dictionary in a way that avoids Float64Index. Commented Jul 22, 2019 at 21:34

1 Answer 1

1

Your data frame looks a little crazy, but here's a try:

df = pd.DataFrame({0z: {44: 492.0, 45: 492.0},
 1: {44: pd.Float64Index([506.76], dtype='float64'),
  45: pd.Float64Index([506.76], dtype='float64')},
 2: {44: pd.Float64Index([516.8952], dtype='float64'),
  45: pd.Float64Index([516.8952], dtype='float64')},
 3: {44: pd.Float64Index([527.233104], dtype='float64'),
  45: pd.Float64Index([527.233104], dtype='float64')},
 4: {44: pd.Float64Index([537.77776608], dtype='float64'),
  45: pd.Float64Index([537.77776608], dtype='float64')}}
)

df.stack().apply(lambda x: pd.Series(x))[0].unstack()

Output:

        0       1         2           3           4
44  492.0  506.76  516.8952  527.233104  537.777766
45  492.0  506.76  516.8952  527.233104  537.777766
Sign up to request clarification or add additional context in comments.

Comments

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.