admin管理员组

文章数量:1289602

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 :

The result what I mean :

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 :

The result what I mean :

Share Improve this question asked Feb 20 at 7:43 Gzai KunGzai Kun 1701 silver badge12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

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

Assuming that you want to merge one empty DataFrame with one column named "Category: A", and the other non-empty DataFrame with columns "Date" and "Value", I would do it like this:

import pandas as pd

# 1st DataFrame with single column
df1 = pd.DataFrame(columns=['Category: A'])
# 2nd DataFrame with double columns 
data = [
  ["Date", "Value"],
  ['2025-01-01', 50],
  ['2025-01-02', 40],
  ['2025-01-03', 45]
]
df2 = pd.DataFrame(data[1:], columns=data[0])

# Merge the two DataFrames
result = pd.concat([df1, df2], axis=1)
print(result)
result.to_csv('/content/test.csv', index=False, header=False)

Output:

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

本文标签: