简体   繁体   中英

3 dimensional numpy array to pandas dataframe

I have a 3 dimensional numpy array

   ([[[0.30706802]],

   [[0.19451728]],

   [[0.19380492]],

   [[0.23329106]],

   [[0.23849282]],

   [[0.27154338]],

   [[0.2616704 ]], ... ])

with shape (844,1,1) resulting from RNN model.predict()

y_prob = loaded_model.predict(X)

, my problem is how to convert it to a pandas dataframe. I have used Keras

my objective is to have this:

0      0.30706802
7      0.19451728
21     0.19380492
35     0.23329106
42       ...
         ...   
815      ...
822      ...
829      ...
836      ...
843      ...
Name: feature, Length: 78, dtype: float32

idea is to first flatten the nested list to list than convert it in df using from_records method of pandas dataframe

import numpy as np
import pandas as pd

data = np.array([[[0.30706802]],[[0.19451728]],[[0.19380492]],[[0.23329106]],[[0.23849282]],[[0.27154338]],[[0.2616704 ]]])

import itertools
data  = list(itertools.chain(*data))
df = pd.DataFrame.from_records(data)

Without itertools

data = [i for j in data for i in j]
df = pd.DataFrame.from_records(data)

Or you can use flatten() method as mentioned in one of the answer, but you can directly use it like this

pd.DataFrame(data.flatten(),columns = ['col1']) 

Here you go!

import pandas as pd
y = ([[[[11]],[[13]],[[14]],[[15]]]])
a = []
for i in y[0]:
    a.append(i[0])
df = pd.DataFrame(a)
print(df)

Output:

    0
0  11
1  13
2  14
3  15

Feel free to set your custom index values both for axis=0 and axis=1.

You could try:

s = pd.Series(your_array.flatten(), name='feature')

https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.html

You can then convert the series to a dataframe using s.to_frame()

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