dataframe只打印第一行,编写csv(pandas.DataFrame.to_csv)时跳过第一行

In my python script I am reading a csv file via

df = pd.read_csv('input.csv', sep=';', encoding = "ISO-8859-1", skiprows=2, skipfooter=1, engine='python')

I am the skipping the first two rows in the csv file because they are just discriptions I don't need.

After importing, I am filtering and separating the data. I want to write the data back to csv files while having the same format as before (first two rows either empty or the description as before the import). How can I do that?

Currently I am using

df.to_csv('output.csv'), sep=';', encoding = "ISO-8859-1")

Is there something like a parameter "skiprows" for exporting? I can't find one in the api documentation for .to_csv.

解决方案

One possible solution is write DataFrame with NaNs first and then append original DataFrame:

df1 = pd.DataFrame({'a':[np.nan] * 2})

df1.to_csv('output.csv', index=False, header=None)

df.to_csv('output.csv', sep=';', encoding = "ISO-8859-1", mode='a')

Or same original header to df1 and this write first, only necessary no value | in header data:

df1 = pd.read_csv('input.csv', sep='|', encoding = "ISO-8859-1", nrows=2, names=['tmp'])

df1.to_csv('output.csv', index=False, header=None)

df.to_csv('output.csv', sep=';', encoding = "ISO-8859-1", mode='a')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值