简体   繁体   中英

From pandas dataframe to multidimensional numpy array for compatibility with tensorflow

I have a pandas dataframe where one of the columns contains lists:

import pandas as pd
import numpy as np
(
   pd.DataFrame({
      "x": [[1, 2], [3, 4], [5, 6]]
   )}
   .assign(x = lambda data: data.x.apply(np.array))  # convert lists into numpy arrays
   .to_numpy()
   .shape  # returns (3, 1) when I was hoping for a (3,1,2)
)

I would like to pass this data into tensorflow as a 3D array, but first I need to be able to get the right shape out of it.

Many thanks!

You could retrieve it like:

import pandas as pd
import numpy as np
npArr = np.array(pd.DataFrame({"x": [[1, 2], [3, 4], [5, 6]],
                               "y": [[1, 2], [3, 4], [5, 6]]}).values.tolist())
print(npArr.shape)

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