Home > front end >  how can change numpy array to single value?
how can change numpy array to single value?

Time:09-22

I loaded mat file and I get followed dictionary.

key Value
A [[Numpy array],[Numpy array],...]
B [[Numpy array],[Numpy array],...]
... ...

So, I changed this dictionary to Dataframe using X = pd.Dataframe.from_dict(Y)

Index A B ...
0 [array(0.)] [array(10.)] ...
1 [array(1.)] [array(11.)] ...
... ... ... ...

But, I expect to followed dataframe.

Index A B ...
0 0. 10. ...
1 1. 11. ...
... ... ... ...

(The single value type whether int or float is not important to me.)

How can change numpy array to single value?

Thanks for your help.

CodePudding user response:

It looks like the elements in your dataframe are lists with a single element. To get rid of that, you can simply use the pandas method explode. So in your case, X = X.explode(['A','B']) for all your columns. If you still struggle having numpy arrays instead of floats, X = X.astype(float) should do the trick.

  • Related