简体   繁体   English

使用 Python pandas 将空白行插入 csv

[英]Insert a blank row into a csv using Python pandas

I'm looking to insert a blank row to the top of a csv.我希望在 csv 的顶部插入一个空白行。 I can't figure out how to do this?我不知道该怎么做?

#df_marks = df_marks.append(new_row, ignore_index=True)

#header_list = ["","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]
#ordersUnleashed = pd.read_csv("ClientOrders.csv", names=header_list)

#insert empty row up top
#ordersUnleashed.iloc[0] = [''] * 30
#ordersUnleashed.loc['0'] = ['','','','','','','','','','','','','','','','','','','','','','','','',''

#set headers or move rows down one and add columns
ordersUnleashed.columns = ["*Order Number", "*Customer Code", "Tax Code", "Tax Rate", "Exchange Rate", "Customer Reference", "Comments", "Order Date (DD/MM/YYYY)", "Quote Expiry Date (DD/MM/YYYY)", "Required Date (DD/MM/YYYY)", "Warehouse Code", "Delivery Contact", "Delivery Name", "Delivery Street Address", "Delivery Street Address 2", "Delivery Suburb", "Delivery City", "Delivery Region", "Delivery Post Code", "Delivery Country", "Delivery Instruction", "Discount (%)", "Sales Order Group", "Sales Person Email", "*Line Number"," *Product Code", "*Order Quantity", "Line Discount (%)", "Unit Price", "Line Comments"]
df.head()

I don't believe the functionality exists in pandas.我不相信 pandas 中存在该功能。 to_csv() does accept any file object though, so you can simply write a line to the file and then have pandas append to the bottom of this open file. to_csv()确实接受任何文件 object ,因此您可以简单地在文件中写入一行,然后将 pandas append 放在此打开文件的底部。

with open('my_file.csv', 'w') as f:
  f.write('\n')
  df.to_csv(path_or_buf=f, index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM