aLLex aLLex - 1 month ago 30
Python Question

Create Panel from DataFrame

I am a new to python and pandas. And I have a problem in creating Panel at pandas.

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)


I'm getting error 'int' object has no attribute 'shape'

I tried with list

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)


But still getting same error

Answer

Pandas is trying to access w1 as a dataFrame but it's an int. So of course it does not have a shape attribute

You should write 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)

Thus Pandas will accept it as a dictionary with integer keys and DataFrame values.

Comments