Search code examples
pythondataframecsvline

How to add line breaks after each comma of a csv file?


I have a csv - file like this:

1.149, 1.328, 1.420, 1.148

and that's my current code:

import pandas as pd
df = pd.read_csv("right.csv")

Python works on columns and rows for output. But I would like to have such an output:

1.149,
1.328, 
1.420, 
1.148

I need it that way, because afterwards I want to know how much data is in the CSV file and work with it. But now it just tells me that I have one row and 4 column.

Could someone help me please?


Solution

  • From my understanding, there is only one row of data like the one you had shown as an example:

    1.149, 1.328, 1.420, 1.148
    

    You can replace the white space with a new line \n.

    import pandas as pd
    df = pd.read_csv("right.csv")
    print(df.replace(", ", ",\n"))
    

    Which will give you the result you are expecting according to my understanding:

    1.149,
    1.328,
    1.420,
    1.148