Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Appending a DataFrame to a numpy array of DataFrames

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading