Python Forum
[Solved] df.to_excel: certain rows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] df.to_excel: certain rows
#1
I would like to write only a few rows from a dataframe into an Excel file.
Is this possible directly when writing or do I have to define a new variable first?


import pandas as pd
import numpy as np

d = {'col1': [1,2,3,4,5,6], 'col2': [1,2,3,4,5,6]}
df = pd.DataFrame(data=d)
df

with pd.ExcelWriter('minimal_to_excel.xlsx') as writer:  
    df.to_excel(writer, startrow=1, sheet_name='row_1_to_3')
Reply
#2
One way is to feed .to_excel with filtered dataframe. For first three rows it would be:

df.iloc[:3].to_excel(writer, startrow=1, sheet_name='row_1_to_3')  
ju21878436312 likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(May-26-2021, 06:21 AM)perfringo Wrote: One way is to feed .to_excel with filtered dataframe. For first three rows it would be:

df.iloc[:3].to_excel(writer, startrow=1, sheet_name='row_1_to_3')  

Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas .to_excel() Dropping First Row of Data When Run from Anaconda Prompt kazoli 0 2,339 Mar-15-2018, 04:52 PM
Last Post: kazoli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020