i have a numpy array that has values for each column in each subarray [[column one info], [column2 info], [column3 info]] i have tried this
df = pd.DataFrame(data = tarray, index=tindex, columns=column_values)
i have also tried this
df = pd.DataFrame(tarray, tindex, column_values)
this is the entire code block
import numpy as np
import pandas as pd
tzip = 76000
tname = ['rest1', 'rest2', 'rest3', 'rest4']
taddy = ['1234 main', '1235 main', '1236 main', '1237 main']
column_values = ['zipcode', 'restaurant_name', 'address']
tzip_arr = []
tindex = []
for x in range(len(tname)):
tindex.append(x)
tzip_arr.append(tzip)
tarray = np.array([tzip_arr,tname,taddy])
df = pd.DataFrame(tarray, tindex, column_values)
print(df)
now finally the error im getting is "ValueError: Shape of passed values is (3, 4), indices imply (4, 3)"
CodePudding user response:
Possible solution is the following:
import numpy as np
import pandas as pd
tzip = 76000
tname = ['rest1', 'rest2', 'rest3', 'rest4']
taddy = ['1234 main', '1235 main', '1236 main', '1237 main']
column_values = ['zipcode', 'restaurant_name', 'address']
tzip_arr = []
for x in range(len(tname)):
tzip_arr.append(tzip)
tarray = np.array([tzip_arr,tname,taddy])
df = pd.DataFrame(data=tarray.T, columns=column_values)
df
Returns
CodePudding user response:
The problem was that you didnt have the np array in the right shape, therefore you need to reshape it like that
df = pd.DataFrame(tarray.reshape(3,4).transpose(), tindex, column_values)