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 badges2 Answers
Reset to default 2There 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
本文标签:
版权声明:本文标题:python - Pandas merge single column with double column DataFrame without commas for single column in csv - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741453036a2379596.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论