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')

你可能感兴趣的:(dataframe只打印第一行)