简体   繁体   中英

create pandas df from numpy arrya

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)

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

在此处输入图像描述

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)

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