Mr. Caribbean
Mr. Caribbean

Reputation: 124

Does Polars module not have a method for appending DataFrames to output files?

When writing a DataFrame to a csv file, I would like to append to the file, instead of overwriting it.

While pandas DataFrame has the .to_csv() method with the mode parameter available, thus allowing to append the DataFrame to a file, None of the Polars DataFrame write methods seem to have that parameter.

Upvotes: 6

Views: 5447

Answers (2)

Pedja
Pedja

Reputation: 1

You can also convert Polars df to pandas df using .to_pandas() method, and then save to csv with mode="a+".

Upvotes: 0

jqurious
jqurious

Reputation: 21184

To append to a CSV file for example - you can pass a file object e.g.

import polars as pl

df1 = pl.DataFrame({"a": [1, 2], "b": [3 ,4]})
df2 = pl.DataFrame({"a": [5, 6], "b": [7 ,8]})

with open("out.csv", mode="a") as f:
   df1.write_csv(f)
   df2.write_csv(f, include_header=False)
>>> from pathlib import Path
>>> print(Path("out.csv").read_text(), end="")
a,b
1,3
2,4
5,7
6,8

Upvotes: 8

Related Questions