pythonpandasexport-to-csv

pandas how to add details to csv before writing a data frame


I need to write to a csv file that has 3-5 lines(rows) of details about the file, followed by 3 rows of blank lines before I can append a data frame.

Here is how the file should look like. (Note: lines having '#' are comments for demonstration)

some details
some more details
some details that were not covered in last two details
#blankline1
#blankline2
#blankline3
A1;B;C  #headers
1231;1241;abc
1232;1242;abd
1233;1243;abe
1234;1244;abf
.
.
.

Here is what I've tried so far:

import pandas as pd

file_name = 'my_csv_file.csv'

df1 = pd.Dataframe({"some details":})
df2 = pd.DataFrame({"some more details":})
df3 = pd.DataFrame({"some details that were not covered in last two details'})
df4 = pd.DataFrame({"\n\n\n\":}) #write 3 blank lines
df5 = pd.DataFrame({"A":[1231 1232 1233 1234]
                    "B":[1241 1242 1243 1244]
                   "headers":[abc abd abe abf] }

df1.to_csv(file_name, sep=';', mode='a', index=False)
df2.to_csv(file_name, sep=';', mode='a', index=False)
df3.to_csv(file_name, sep=';', mode='a', index=False)
df4.to_csv(file_name, sep=';', mode='a', index=False)
df5.to_csv(file_name, sep=';', mode='a', index=False)

but this doesn't seem to work.


Solution

  • You could use a string template and leave {} to insert the dataframe into. The whole thing becomes more readable. Useful things to do: 1) Use """ for multi-line strings and 2) Use comments to remember why you do something.

    import pandas as pd
    
    df = pd.DataFrame({
        "A": [1231, 1232, 1233, 1234],
        "B": [1241, 1242, 1243, 1244],
        "C": ['abc', 'abd', 'abe', 'abf']
    })
    
    # Setups the template that the client requested with 3-5 rows of information 
    # Followed by 3 blank rows and the dataframe
    template = """\
    some details
    some more details
    some details that were not covered in last two details
    
    
    
    {}"""
    
    with open('test.txt', 'w') as fp:
        fp.write(template.format(df.to_csv(index=False)))
    

    test.csv:

    some details
    some more details
    some details that were not covered in last two details
    
    
    A,B,C
    1231,1241,abc
    1232,1242,abd
    1233,1243,abe
    1234,1244,abf
    

    Note: Data from user Taras