WRITE only first N rows from pandas df to csv
问题 How can I write only first N rows or from P to Q rows to csv from pandas dataframe without subseting the df first? I cannot subset the data I want to export because of memory issues. I am thinking of a function which writes to csv row by row. Thank you 回答1: Use head- Return the first n rows. Ex. import pandas as pd import numpy as np date = pd.date_range('20190101',periods=6) df = pd.DataFrame(np.random.randn(6,4), index=date, columns=list('ABCD')) #wtire only top two rows into csv file print