当多个带表头的DataFrame使用to_csv追加时,表头重复的问题。
全部显示
pd.options.display.max_columns = None
pd.options.display.max_rows = None
例如:

追加的时候,判断文件是否存着,如果不存在则添加表头,否则不添加表头
if not os.path.exists('result.csv'):
data.to_csv('result.csv',encoding='utf_8_sig',mode='a',index=False,index_label=False)
else:
data.to_csv('result.csv', encoding='utf_8_sig', mode='a', index=False, index_label=False,header=False)
本文介绍了一种在Python中使用pandas库将多个带有表头的数据框追加到同一个CSV文件中的方法,通过判断文件是否存在来决定是否写入表头,避免了表头重复出现的问题。
5834





