How to write data to excel with header using Pandas?

2024/7/7 7:20:02

I want to write the excel file with the header is enter image description here

I have data and I want to fill out the excel file. Then the expected result is enter image description here

This is my code but it does not show as my expected. Could you help me to fix it? Thanks

from pandas import DataFrame
id =['1','2']
width_man=['12','18']
height_man=['20','55']
age_man=['10','12']
width_woman=['15','11']
height_woman=['40','45']
age_woman=['11','15']df = DataFrame({'ID': id, 'WIDTH': width_man, 'HEIGHT': height_man, 'AGE': age_man, 'WIDTH': width_woman, 'HEIGHT': height_woman, 'AGE': age_woman,})
df.to_excel('table.xlsx', sheet_name='sheet1', index=False)

THIS IS OUTPUT OF MY CODE

enter image description here

Answer

Use below method,

import pandas as pdid =['1','2']
width_man=['12','18']
height_man=['20','55']
age_man=['10','12']
width_woman=['15','11']
height_woman=['40','45']
age_woman=['11','15']df1 = pd.DataFrame({'ID': id, 'WIDTH': width_man, 'HEIGHT': height_man, 'AGE': age_man})
df2 = pd.DataFrame({'ID': id, 'WIDTH': width_woman, 'HEIGHT': height_woman, 'AGE': age_woman})
writer = pd.ExcelWriter('table.xlsx')
df1.to_excel(writer, sheet_name='Sheet1', startrow=1, index=False) 
df2.to_excel(writer, sheet_name='Sheet1',startrow=1,startcol=5, index=False)
worksheet = writer.sheets['Sheet1']
worksheet.write_string(0, 2, 'MAN')
worksheet.write_string(0, 6, 'WOMAN')writer.save()
https://en.xdnf.cn/q/120143.html

Related Q&A

Cooldown format in days discord.py

Im currently making a command which has a cooldown of 5 days, and Im currently using this code for the cooldown. def better_time(self, cd:int):time = f"{cd}s"if cd > 60:minutes = cd - (cd …

From scraper_user.items import UserItem ImportError: No module named scraper_user.items

I am following this guide for scraping data from instagram: http://www.spataru.at/scraping-instagram-scrapy/ but I get this error:mona@pascal:~/computer_vision/instagram/instagram$ ls instagram scrap…

Correct way of coding a Guess the Number game in Python [closed]

Closed. This question is off-topic. It is not currently accepting answers.Want to improve this question? Update the question so its on-topic for Stack Overflow.Closed 11 years ago.Improve this questio…

How do i stop the infinite loop for the if and elif part?

c = random.randint(0,5) guess =int(input("=")) while True:if guess > c:print("you failed the test")elif guess <c: print("you messed up")else:print("you are the …

How to zip a list of lists

I have a list of listssample = [[A,T,N,N],[T, C, C, C]],[[A,T,T,N],[T, T, C, C]].I am trying to zip the file such that only A/T/G/C are in lists and the output needs to be a list[[AT,TCCC],[ATT,TTCC]]W…

I want to make a Guess the number code without input

import random number = random.randint(1, 10)player_name = "doo" number_of_guesses = 0 print(I\m glad to meet you! {} \nLet\s play a game with you, I will think a number between 1 and 10 then …

How to zip keys within a list of dicts

I have this object: dvalues = [{column: Environment, parse_type: iter, values: [AirportEnclosed, Bus, MotorwayServiceStation]}, {column: Frame Type, parse_type: list, values: [All]}]I want a zipped out…

AttributeError: DataFrame object has no attribute allah1__27

Im trying to solve this and Im pretty sure the code is right but it keeps getting me the same Error.I have tried this:import datetime from datetime import datetime as datettest_df = shapefile.copy() te…

How to convert csv to dictionary of dictionaries in python?

I have a CSV file shown below.I need to convert CSV to dictionary of dictionaries using python.userId movieId rating 1 16 4 1 24 1.5 2 32 4 2 47 4 2 …

Mo Money- Making an algorithm to solve two variable algebra problems

A cash drawer contains 160 bills, all 10s and 50s. The total value ofthe 10s and 50s is $1,760.How many of each type of bill are in the drawer? You can figure thisout by trial and error (or by doing a…