How to export pandas dataframe column names into CSV ( 2 Steps )?

How to export pandas dataframe column names into CSV

In many data engineering tasks, We have to deal with a large number of columns. Sometimes we need to share the column-level information with stakeholders. In this situation, we can export pandas dataframe column names into CSV. In this article, we will explore the best way to export pandas dataframe column names.

 

Export pandas dataframe column names into CSV ( Steps ) –

As a prerequisite for this, There will be step 0, where we will create a dummy dataframe.

Step 0 ( Prerequisiste ) –

Here is a sample dataframe with some sample data. We will use this dataframe to walk through the solution.

import pandas as pd

column1 = [1, 2, 3, 4, 5]
column2 = ['A', 'B', 'C', 'D', 'E']
column3 = [0.1, 0.2, 0.3, 0.4, 0.5]
column4 = [True, False, True, False, True]
column5 = [10, 20, 30, 40, 50]

dict_data = {
    'A': column1,
    'B': column2,
    'C': column3,
    'D': column4,
    'F': column5
}

df = pd.DataFrame(dict_data)
sample dataframe for column name export as CSV
sample dataframe for column name export as CSV

Step 1 exporting column names into a list –

As a first step, we will create a list-type object containing all pandas dataframe column names. Here is the code for that

column_name_list = list(df.columns)

Step 2 exporting column names list into CSV –

This is the final step where we will export column names into CSV. To achieve this we will create a temporary dataframe from the above list and then finally export the same into CSV format. Here is the code for that.

column_names_df = pd.DataFrame({'Column Names': column_name_list})
column_names_df.to_csv('column_names.csv', index=False)

When we run the above code it will create a ‘column_names.csv’ named CSV at the same directory level where we are executing this code.

column name export as CSV
column name export as CSV

 

Addition column level information with column name in export –

Suppose we have one dataframe with all nuemric data. We want to extract the column name with thier mean() value. Then simply we will apply the same logic as above but along with this we will add one more column and then create a dataframe with these list and then finally we can export as CSV. Here is the example for this.

import pandas as pd

column1 = [1, 2, 3, 4, 5]
column2 = [0.1, 0.2, 0.3, 0.4, 0.5]
column3 = [10, 20, 30, 40, 50]

dict_data = {
    'A': column1,
    'B': column2,
    'C': column3}

df = pd.DataFrame(dict_data)
mean_list=list(df.mean().values)
column_name_list = list(df.columns)
column_names_df = pd.DataFrame({'Column Names': column_name_list,'Column mean': mean_list}) 
column_names_df.to_csv('data.csv', index=False)

Thanks
Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner