-1

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.

1 Answer 1

0

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.

3
  • Thank you very much!!! I can solve this problem thanks to your advice. Using X=X.apply(pd.Series.explode), all columns in X Dataframe change a single value. Thank you!
    – snuuser
    Sep 21, 2022 at 8:54
  • Great to hear! It would be nice if you could accept my answer if it solved your problem :)
    – Nyquist
    Sep 21, 2022 at 8:56
  • I use this stackoverflow for the first time, so I'm not used to it. :( I think I accepted your answer now! If not, please leave a comment again!
    – snuuser
    Sep 22, 2022 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.