pythonappendpython-polarswritefile

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.


Solution

  • 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