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’)
Original: https://blog.csdn.net/weixin_39665762/article/details/112889457
Author: weixin_39665762
Title: dataframe只打印第一行_编写csv(pandas.DataFrame.to_csv)时跳过第一行
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/753061/
转载文章受原作者版权保护。转载请注明原作者出处!