从 numpy 数组创建 pandas df
create pandas df from numpy arrya
我有一个 numpy 数组,每个子数组中的每一列都有值 [[column one info], [column2 info], [column3 info]]
我试过这个:
df = pd.DataFrame(data = tarray, index=tindex, columns=column_values)
这个我也试过了
df = pd.DataFrame(tarray, tindex, column_values)
这是整个代码块
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)
现在我得到的错误终于是 ValueError: Shape of passed values is (3, 4), indices imply (4, 3)
可能的解决方案如下:
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
问题是你没有正确形状的 np 数组,因此你需要像那样重塑它
df = pd.DataFrame(tarray.reshape(3,4).transpose(), tindex, column_values)
我有一个 numpy 数组,每个子数组中的每一列都有值 [[column one info], [column2 info], [column3 info]]
我试过这个:
df = pd.DataFrame(data = tarray, index=tindex, columns=column_values)
这个我也试过了
df = pd.DataFrame(tarray, tindex, column_values)
这是整个代码块
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)
现在我得到的错误终于是 ValueError: Shape of passed values is (3, 4), indices imply (4, 3)
可能的解决方案如下:
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
问题是你没有正确形状的 np 数组,因此你需要像那样重塑它
df = pd.DataFrame(tarray.reshape(3,4).transpose(), tindex, column_values)