从DataFrame创建面板

时间:2016-10-20 10:11:15

标签: python pandas dataframe

我是python和pandas的新手。我在pandas中创建Panel时遇到了问题。

def sq_error(w0,w1,x,y):
    return (y - (w0 - w1 * x)) ** 2;


d = dict()
w0 = 50
for w1 in range(0, 10):
    data['height_error'] = data.apply(lambda row: sq_error(w0,w1,row['Weight'], 
                                              row['Height']), axis=1)
    df = data[['height_error','Weight']]
    d['df'] =  df
    d['w1'] = w1


p = pd.Panel(d)

我收到错误' int'对象没有属性'形状'

我尝试了列表

d = dict()
w0 = 50
for w1 in range(0, 10):
    data['height_error'] = data.apply(lambda row: sq_error(w0,w1,row['Weight'], 
                                              row['Height']), axis=1)
    l = df[['height_error','Weight']].values.tolist()
    d['df'] = l
    d['w1'] = w1

p = pd.Panel(d)

但仍然得到同样的错误

1 个答案:

答案 0 :(得分:0)

Pandas试图将w1作为dataFrame访问,但它是一个int。所以当然它没有形状属性

你应该写d [w1] = df

d = dict()
w0 = 50
for w1 in range(0, 10):
    data['height_error'] = data.apply(lambda row: sq_error(w0,w1,row['Weight'],row['Height']), axis=1)
    l = df[['height_error','Weight']].values.tolist()
    d[w1] = df

p = pd.Panel(d)

因此,Pandas会将其接受为具有整数键和DataFrame值的字典。