I need to update a panel slice with some values retreated from a dataframe. Even if I don't get back any error it doesn't work. What it's wrong ?
df = pd.DataFrame(np.random.rand(10, 4),
columns=['sd', 'ed', 'sbc', 'ssd'],
index=np.arange(2000, 2010))
siPanel = pd.Panel(np.nan, items=np.arange(1998, 2014), major_axis=range(0, 10), minor_axis=range(0, 5))
spiPanel.loc[:, [0], [0]] = df.loc[:, ['sbc']]
Be aware that the dimension it's not the same, I would like to use the index to align data. I know that converting to an array with the same shape I can obtain what I want but I would like to find another solution.
You're getting into some semi-complicated dimensional issues.
Let's break down your assignment line a bit.
siPanel.loc[:, [0], [0]] = df.loc[:, ['sbc']]
df.loc[:, ['sbc']]
is a dataframe with a shape of 10 x 1
. :
gave it the 10
and ['sbc']
gave it the 1
. had you done df.loc[:, 'sbc']
the shape would be 10 x 0
or (10, )
, this would be a series. I'd say a good rule to follow would be to use as few dimensions as needed.
siPanel.loc[:, [0], [0]]
is a panel of shape 16 x 1 x 1
. Using the lesson in the prior paragraph, you know we can reduce the dimensions to (16, )
by using siPanel.loc[:, 0, 0]
. This is now a series as well and the assignment can go off without issues.
Try this:
siPanel.loc[:, 0, 0] = df.loc[:, 'sbc']
siPanel.loc[:, 0, 0]
1998 NaN
1999 NaN
2000 0.677186
2001 0.030134
2002 0.003945
2003 0.894504
2004 0.913937
2005 0.553544
2006 0.199962
2007 0.384449
2008 0.491008
2009 0.408632
2010 NaN
2011 NaN
2012 NaN
2013 NaN
Name: 0, dtype: float64