Home > Enterprise >  Appending a DataFrame to a numpy array of DataFrames
Appending a DataFrame to a numpy array of DataFrames

Time:01-12

How do I keep the 3d array structure and not have arr turn into a 1d array?

data = pd.DataFrame([[2,4,6], [7,8,9], [120, 130, 140]])
data1 = pd.DataFrame([[3,3,3], [3,3,3], [3, 3, 3]])

arr = np.array([data])
print(arr)

arr = np.append(arr, data1)
print(arr)

output:

[[[  2   4   6]

  [  7   8   9]

  [120 130 140]]]

[  2   4   6   7   8   9 120 130 140   3   3   3   3   3   3   3   3   3]

CodePudding user response:

You want to replace np.append with np.vstack with accepts 1 tuple and not many arguments

import pandas as pd 
import numpy as np

data = pd.DataFrame([[2,4,6], [7,8,9], [120, 130, 140]])
data1 = pd.DataFrame([[3,3,3], [3,3,3], [3, 3, 3]])

arr = np.array([data])
arr1 = np.array([data1])

result = np.vstack( (arr,arr1))
print(result.shape)
print(result)

or just type

data.append(data1,ignore_index=True)
  •  Tags:  
  • Related