Pandas Remove Columns & Index | Writing CSV File

Use param header=False to remove columns and use index=False to remove index while writing DataFrame to CSV file. Let’s see examples of how you can use these on the to_csv() method. This method also supports several other params to write pandas DataFrame to CSV file.

1. Remove Header Columns While writing CSV File

By default writing a DataFrame to CSV includes header column names on the first row, row index on the first column, and comma-separated delimiter to separate columns.


# Create a DataFrame
import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Hadoop","Python"],
    'Fee' :[22000,25000,np.nan,24000],
    'Duration':['30day',None,'55days',np.nan],
    'Discount':[1000,2300,1000,np.nan]
          }
df = pd.DataFrame(technologies)

To write DataFrame to CSV without column header use header=False param on to_csv() method.


# Remove header while writing
df.to_csv("c:/tmp/courses.csv",header=False)

Writes courses.csv file as.


0,Spark,22000.0,30day,1000.0
1,PySpark,25000.0,,2300.0
2,Hadoop,,55days,1000.0
3,Python,24000.0,,

2. Remove Row Index While Writing CSV File

In the above example, you see the index as the first column right, let’s see how to remove this while writing CSV file. We should use index=False.


# Remover column header and index
df.to_csv("c:/tmp/courses.csv",header=False, index=False)

Writes courses.csv file as.


Spark,22000.0,30day,1000.0
PySpark,25000.0,,2300.0
Hadoop,,55days,1000.0
Python,24000.0,,

Conclusion

In this quick article, you have learned how to remove header and row index while writing DataFrame to CSV file. Use param header=False to remove columns and use index=False to remove index on to_csv() method.

Happy Learning !!

You May Also Like

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

Pandas Remove Columns & Index | Writing CSV File