Reputation: 373
I need to add a single cell to df but before column names(see image below/Example). Is it possible via pandas? Had two ideas, to merge/concat/append two df vertically, of which one would only be a single cell or to df.loc
a single cell, but no luck.
df = pd.DataFrame({'Construction Type': [combo2.get()],'Panel Type': [int(upis_tickness)]}, columns['Construction Type', 'Panel Type'])
df_cell = pd.DataFrame([['Panel Schedule - Manufacturing']])
result_df = pd.concat([df, df_cell], axis=1)
Upvotes: 2
Views: 1049
Reputation: 862921
Close, what you need is MultiIndex
:
mux = pd.MultiIndex.from_product([['Panel Schedule - Manufacturing'],
['Construction Type', 'Panel Type']])
df = (pd.DataFrame({'Construction Type': [combo2.get()],
'Panel Type': [int(upis_tickness)]})
.reindex(columns=mux, level=1))
Sample:
mux = pd.MultiIndex.from_product([['Panel Schedule - Manufacturing'],
['Construction Type', 'Panel Type']])
df = (pd.DataFrame({'Construction Type': [1,2],'Panel Type': [5,9]})
.reindex(columns=mux, level=1))
print (df)
Panel Schedule - Manufacturing
Construction Type Panel Type
0 1 5
1 2 9
EDIT:
If want write to excel to first row is possible use:
df = pd.DataFrame({'Construction Type': [1,2],'Panel Type': [5,9]})
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', startrow = 1, index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
text = 'Panel Schedule - Manufacturing'
worksheet.write(0, 0, text)
writer.save()
Upvotes: 2