Panda's Write CSV - Append vs. Write

后端 未结 3 677
眼角桃花
眼角桃花 2020-11-30 00:10

I would like to use pd.write_csv to write \"filename\" (with headers) if \"filename\" doesn\'t exist, otherwise to append to \"filename\" if it exists. If I simply use co

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 01:12

    with open(filename, 'a') as f:
        df.to_csv(f, mode='a', header=f.tell()==0)
    

    it will add header when writes to the file first time

提交回复
热议问题