I am struggling to append a copy of a DataFrame view into new rows of another dataframe.
On the following table:
Description, Item Number, Company A, Company B, Company C
Assets, 1, 100000, 50000, 20000
Liabilities, 2, 80000, 10000, 15000
I would like to recreate as
Description, Item Number, Company, Value
Assets, 1, Company A, 100000
Liabilities, 2, Company A, 80000
Assets, 1, Company B, 50000
Liabilities, 2, Company B, 10000
Assets, 1, Company C, 20000
Liabilities, 2, Company C, 15000
Code used
ll=list(range(2,df.shape[1]))
retobj = pd.DataFrame(columns=[df.columns[0:2]]) #create dataframe to return
for item in ll:
nl=[0,1]
nl.append(item)
tdf=df.iloc[:,nl].copy() #tdf-temporary data frame
prevheading=tdf.columns[-1]
tdf.rename(columns = {prevheading:'Value'}, inplace=True)
tdf['Company'] = prevheading
retobj.append(tdf)
It copies the first company correctly but then fails with the following error: AttributeError: 'NoneType' object has no attribute 'is_extension'.
The reason for this approach is that the data received is variable in terms of columns. i.e. multiple companies can be received.