Feature Type

  • [X] Adding new functionality to pandas

  • [ ] Changing existing functionality in pandas

  • [ ] Removing existing functionality in pandas

Problem Description

When writing to csv, sometimes we want to skip rows. The most useful use case is when someone want to write multiple dataframes in single csv files useing mode='a'. If pandas support skip rows, we can give an empty rows to the file. Later in reading, we can use a detect NaN as separator between tables.

I prefer skiprows than startrow as the most relevant use case is to skip rows and write from there.

Feature Description

def  to_csv(skiprows=N):
    # this feature works like read from first line, skipping N rows using readline, use write delimiter only in case no more read
    ...


def  to_csv(startrow=N):
    # this feature works by writing delimiter and change lines from the start N time.
    for i in range(N):
        write(f"{delimiter}\n)
    ...

Alternative Solutions

-

Additional Context

No response

Comment From: jgarba

Take

Comment From: yasirroni

*edited the first example to skiprows

Comment From: MarcoGorelli

hi @yasirroni

what would this add on top of df.iloc[N:].to_csv?

Comment From: yasirroni

@MarcoGorelli The one that is skipped is the row in CSV itself, not the dataframe.

Comment From: MarcoGorelli

what would be the difference?

Comment From: yasirroni

@MarcoGorelli assume that you have csv files that already has a 100 rows of data.

Now you have a DataFrame, with different column numbers, and only 5 rows. This dataframe is useful to explain the original 100 rows of data.

You don't want to separate it into two CSV because ideally you will use single XLSX file with two sheets.

pandas.DataFrame.to_csv did not support this.

Comment From: MarcoGorelli

ah sorry, I'd misread - so, you just want some way to write empty rows?

can't you first create an dataframe full of nans of your desired length, write that to csv, and then call to_csv with your given dataframe? could you show a concrete example with expecteed output please?

Comment From: yasirroni

@MarcoGorelli the startrow is indeed like that. But the skiprows works in an existing CSV (existing csv with different column and rows number to dataframe that we want to enter below the CSV).

I've already done the workaround anyway. One way to achieve this js by reading first and make the combined dataframe.

But, it will be nice to have this function.

Comment From: MarcoGorelli

I've already done the workaround anyway. One way to achieve this js by reading first and make the combined dataframe.

this sounds fine to be honest, no need to add even more args - thanks for the suggestion though

Comment From: mroeschke

Agreed I don't think this functionality is worth increasing the API surface area. Thanks but closing

Comment From: yasirroni

But, .to_excel has this API.