Below if my dataframe
html = data.to_html()
I am getting output table as :
by i want output table as :
Code:
data = [[0,65247.0,53306.0,112.0,6524.0,53306.0,112.0], [1,55695.0,43214.0,0.0,5569.0,43214.0,0.0], [2,78345.0,65189.0,0.0,7834.0,65189.0,0.0], [3,16151.0,14057.0,0.0,1615.0,14057.0,0.0],[4,13960.0,12062.0,0.0,1396.0,12062.0,0.0]]
df = pd.DataFrame(data, columns = ['HOUR','SUB','DEL %','WAIT %', 'SUB','DEL %','WAIT %'])
Have duplicate columns isn't a good idea, adding the suffix can help
df.columns = pd.MultiIndex.from_tuples((('', 'HOUR'), ('BSNL_WEST', 'SUB'), ('BSNL_WEST', 'DEL %'), ('BSNL_WEST', 'WAIT %'), ('BSNL_NORTH', 'SUB_2'), ('BSNL_NORTH', 'DEL %_2'), ('BSNL_NORTH', 'WAIT %_2')))
Results in
BSNL_WEST BSNL_NORTH
HOUR SUB DEL % WAIT % SUB_2 DEL %_2 WAIT %_2
0 0 65247.0 53306.0 112.0 6524.0 53306.0 112.0
1 1 55695.0 43214.0 0.0 5569.0 43214.0 0.0
2 2 78345.0 65189.0 0.0 7834.0 65189.0 0.0
3 3 16151.0 14057.0 0.0 1615.0 14057.0 0.0
4 4 13960.0 12062.0 0.0 1396.0 12062.0 0.0
You can try this, i tried it on your data set:
testdf = df
testdf = testdf.drop(columns='HOUR')
columns=[('BSNL_WEST', 'SUB'), ('BSNL_WEST', 'DEL %'), ('BSNL_WEST', 'WAIT %'),('BSNL_NORTH', 'SUB'), ('BSNL_NORTH', 'DEL %'), ('BSNL_NORTH', 'WAIT %') ]
testdf.columns = pd.MultiIndex.from_tuples(columns)
testdf = testdf.rename_axis(columns=['HOUR','HOUR'])
I changed to your dataframe:
HOUR BSNL_WEST BSNL_NORTH
HOUR SUB DEL % WAIT % SUB DEL % WAIT %
0 65247.0 53306.0 112.0 6524.0 53306.0 112.0
1 55695.0 43214.0 0.0 5569.0 43214.0 0.0
2 78345.0 65189.0 0.0 7834.0 65189.0 0.0
3 16151.0 14057.0 0.0 1615.0 14057.0 0.0
4 13960.0 12062.0 0.0 1396.0 12062.0 0.0
You can set the hour as index, but it won't format the same as how an excel spreadheet does:
testdf.rename_axis(index=['HOUR'])
BSNL_WEST BSNL_NORTH
SUB DEL % WAIT % SUB DEL % WAIT %
HOUR
0 65247.0 53306.0 112.0 6524.0 53306.0 112.0
1 55695.0 43214.0 0.0 5569.0 43214.0 0.0
2 78345.0 65189.0 0.0 7834.0 65189.0 0.0
3 16151.0 14057.0 0.0 1615.0 14057.0 0.0
4 13960.0 12062.0 0.0 1396.0 12062.0 0.0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.