Issue
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 I'm getting is ValueError: Shape of passed values is (3, 4), indices imply (4, 3)
Solution
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
Answered By - GreyMurav
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.