简体   繁体   中英

Write dataframe and strings in one single csv file

I want to export a dataframe (500 rows,2 columns) from python to a CSV file.

However, I need to ensure that 1st 20 rows have some text/strings written and then the dataframe(500 rows,2 columns) should start from the 21st row onwards.

I referred to the following link: Skip first rows when writing csv (pandas.DataFrame.to_csv) . However, it does not satisfy my requirements.

Can somebody please let me know how do we do this?

  1. Get first 20 rows and save it to another dataframe
  2. Check if there are any null values
  3. If not any null values, remove first 20 rows
  4. Save df as a csv file

df2 = df.head(20)

df2 = df2.isnull().values.any() 

if not df2:
    df = df[10:]
    df.to_csv('updated.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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