简体   繁体   中英

How to convert pandas series to 2D array?

I have a pandas series that looks like this: 在此处输入图像描述

How can I convert this into a 2D array that looks like this: ([10.32.5.53, 1008],[10.32.5.56,1002]...[10.32.5.58,11]) ?

Since my edits are removed from @RikKraan post, here the answer as I mentioned in the comments to his reply.

First apply reset_index to your series and then use to_numpy , ie

series.reset_index().to_numpy()

Note, reset_index converts the series to a dataframe.

As mentioned in the comments, the OP wants to store the IP as float. This can be done by removing the . in the IP string and converting the index to float before converting as mentioned above. Ie

series.index = series.index.str.replace('.','').astype(float)
series.reset_index().to_numpy()

This will give you your 2D numpy array with dtype float.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM