Search code examples
pythonpandas

Pandas merge single column with double column DataFrame without commas for single column in csv


I have 2 DataFrame with different columns and want to merge to csv without comma for the one having single column.

How can we remove comma for the one having single column?

import pandas as pd

# 1st DataFrame with single column
pd_title = pd.DataFrame(['Category: A', ''])

# 2nd DataFrame with double columns 
data = [
  ["Date", "Value"],
  ['2025-01-01', 50],
  ['2025-01-02', 40],
  ['2025-01-03', 45]
]

result = pd_title._append(data).reset_index(drop=True)

result.to_csv('/content/test.csv', index=False, header=False)

The result from code :

enter image description here

The result what I mean :

enter image description here


Solution

  • There is no direct way to do this in pandas since you're using rows of data as header.

    You could however convert to CSV string and post-process it:

    import re
    
    with open('/content/test.csv', 'w') as f:
        f.write(re.sub(',*\n,+\n', '\n\n', result.to_csv(index=False, header=False)))
    

    A better option would be to first create the output file with the header, then export a dataframe with normal data/header and append it to the file with the mode='a' of to_csv:

    filename = '/content/test.csv'
    with open(filename, 'w') as f:
        f.write('Category: A\n\n')
        
    df = pd.DataFrame(data[1:], columns=data[0])
    #          Date  Value
    # 0  2025-01-01     50
    # 1  2025-01-02     40
    # 2  2025-01-03     45
    
    df.to_csv(filename, index=False, mode='a')
    

    Output:

    Category: A
    
    Date,Value
    2025-01-01,50
    2025-01-02,40
    2025-01-03,45