python

Pandas DataFrame to CSV

# Use custom separator	sep	
df.to_csv('file_name.csv',sep='\t')
# Write without index	index	
df.to_csv('file_name.csv',index=False)
# Write without headers	header	
df.to_csv('file_name.csv',header=False)
# Write subset of columns	columns	
df.to_csv('file_name.csv',columns=['col_A','col_B'])
# Change file encoding format	encoding	
df.to_csv('file_name.csv',encoding='utf-8')
# Change NaNs as Unknown	na_rep	
df.to_csv('file_name.csv',na_rep='Unknown')
Was this helpful?