How to add alternating blank lines when saving csv file in Pandas

Federico Gentile

I have a dataframe and I want to save it to a csv file. This operation is quite simple because I just need to use the following command:

df.to_csv(namefile, sep='', index=False)

The output is a csv file where each line contains the content of the a row of the dataframe. The output is this:

A,B,C,D
1,2,3,4
5,6,7,8
9,1,2,3

However, what I would like to do is to have a blank line every other row so that the output looks like this:

A,B,C,D

1,2,3,4

5,6,7,8

9,1,2,3

Basically I need to add the CR and LF symbol between every other line. Can you suggest me a smart and elegant way to achieve my goal?

MaxU

Use parameter line_terminator='\n\n':

line_terminator : string, default '\n'

The newline character or character sequence to use in the output file

Demo:

df.to_csv(namefile, line_terminator='\n\n')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to skip blank cells when reading a csv file in Java?

How to add pandas data to an existing csv file?

Saving DataFrame names as .csv file names in Pandas

When saving scraped item and file, Scrapy inserts empty lines in output csv file

Prevent atom editor from deleting last blank lines when saving a file

additional column when saving pandas data frame to csv file

how to keep numpy array when saving pandas dataframe to csv

How to nicely read "alternating" lines?

How to read a few lines in a large CSV file with pandas?

How to parse a text file with alternating lines of names and lists of integers?

Read CSV file in Pandas with Blank lines in between

Using Pandas to write file creates blank lines

How to add N blank lines between all rows of a text file?

Saving pandas dataframe as csv and overwrite existing file

Perl deleting "blank" lines from a csv file

How to add blank lines above the bottom in terminal

Blank lines when reading CSV file in Java

saving lines to file based on condition pandas python

removing header and blank lines from a csv file

How to separate a CSV file when there are "" lines?

Remove blank lines in CSV file with GEANY

Python: for loop and saving to a new CSV file with pandas

Why are blank lines being added in CSV file when running python script multiple times?

How Do I Ignore Blank Lines Processing a CSV File In Clojure?

Not saving lines when writing to CSV file using for-loop

How to add blank lines between definitions?

DateTime when saving pandas dataframe to CSV

How to avoid a blank CSV file when uploading to OneDrive?

Extra blank lines in markdown file after submitting and saving in Django

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive