Python Forum
[Solved] df.to_excel: certain rows - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: [Solved] df.to_excel: certain rows (/thread-33768.html)



[Solved] df.to_excel: certain rows - ju21878436312 - May-25-2021

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')



RE: df.to_excel: certain rows - perfringo - May-26-2021

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')  



RE: df.to_excel: certain rows - ju21878436312 - Jun-15-2021

(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!