Erik Dubrovszkij Erik Dubrovszkij -3 years ago 101
Python Question

Import from Excel with panda but missing the index header

I would like to use this data from xlsx :

Fe Mg Al Si P S K Ca Ti
5 0.80 2.09 3.49 7.05 0.19 0.07 0.13 84.28 1.90
5 0.75 2.69 3.91 12.42 0.13 0.09 0.18 78.18 1.64
3 0.87 2.66 3.71 7.64 0.17 0.05 0.12 82.97 1.81
...............


But when I imported :

file = 'test.xlsx'
xl = pd.ExcelFile(file)
df1 = xl.parse('Sheet1')


and than try to give a name to the 0. column but instead I can only renaming the Fe to Fabric:

new_columns = df1.columns.values;
new_columns[0] = 'Fabric';
df1.columns = new_columns


This is what I get:

Fabric Mg Al Si P S K Ca Ti
5 0.80 2.09 3.49 7.05 0.19 0.07 0.13 84.28 1.90
5 0.75 2.69 3.91 12.42 0.13 0.09 0.18 78.18 1.64
3 0.87 2.66 3.71 7.64 0.17 0.05 0.12 82.97 1.81


Instead of this [ :) ] :

Fabric Fe Mg Al Si P S K Ca Ti
5 0.80 2.09 3.49 7.05 0.19 0.07 0.13 84.28 1.90
5 0.75 2.69 3.91 12.42 0.13 0.09 0.18 78.18 1.64
3 0.87 2.66 3.71 7.64 0.17 0.05 0.12 82.97 1.81


I think I missing some basic stuff in here, but when I try to search for this I had no luck, so please If you have an idea for this , help :) . Thank you !

Answer Source

Fabric looks like the index to your dataframe. You are probably best of resetting it and then renaming your columns.

df1 = df1.reset_index()
df1.columns =['Fabric'] + df1.columns[1:].tolist()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download