• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:13 mins read
You are currently viewing Pandas Add Column Names to DataFrame

You can add column names to pandas at the time of creating DataFrame or assign them after creating. Sometimes you might receive a CSV file that doesn’t have names and you would need to add after reading CSV data into DataFrame.

Advertisements

In this article, I will explain how to add/set/assign column names to DataFrame with several examples. The column names on DataFrame are used to identify what type of data each column holds.

1. Quick Examples of Pandas Add Column Names

If you are in a hurry, below are some quick examples of how to add/assign or set column labels to DataFrame.


# Quick examples of pandas add column names

# Example 1: Column names to be added
column_names=["Courses","Fee",'Duration']

# Example 2: Create DataFrame by assigning column names
df=pd.DataFrame(technologies, columns=column_names)

# Example 3: Add column names while reading a CSV file
df = pd.read_csv('courses.csv', names=column_names)

# Example 4: Add column names to existing DataFrame
df.columns = column_names

2. Add Column Names to DataFrame

You can add column names to pandas DataFrame while creating manually from the data object. In order to create a DataFrame, you would use a DataFrame constructor which takes a columns param to assign the names. It takes a list as a value and the number of values in a list should not exceed the number of columns in DataFrame.


# Import pandas library
import pandas as pd
technologies = [ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ]

# Column names to be added
column_names=["Courses","Fee",'Duration']

# Create DataFrame by assigning column names
df=pd.DataFrame(technologies, columns=column_names)
print("Create DataFrame:\n",df)

Yields below output.

pandas add column names

2. Add Names While Reading CSV

pandas read_csv() method has an option to identify the column names that are presented in a CSV file, In case your CSV file doesn’t have on the first row then you can add custom names while reading a CSV into Pandas DataFrame.


# Column names to be added
column_names=["Courses","Fee",'Duration']

# Add column names while reading a CSV file
df = pd.read_csv('courses.csv', names=column_names)

3. Add Column Names to Existing DataFrame

In the above sections, you have seen how to add while creating a DataFrame. Sometimes it’s not possible to know the column names up-front and you may need to add names to the existing DataFrame.

In this example, the columns attribute is used to assign a new list of column names (column_names) to the DataFrame.


import pandas as pd

# Create DataFrame with out column names
df=pd.DataFrame([ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ])

# Assign column names to Existing DataFrame
column_names=["Courses","Fee",'Duration']
df.columns = column_names

print("\nDataFrame with updated column names:")
print(df)

Yields below output.


# Output:
DataFrame with updated column names:
  Courses    Fee Duration
0   Spark  20000   30days
1  Pandas  25000   40days

4. Complete Examples

Below is the complete example of how to assign column names to DataFram.


# Import pandas Library
import pandas as pd
technologies = [ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ]

# Column names to be added
column_names=["Courses","Fee",'Duration']

# Create DataFrame by assigning column names
df=pd.DataFrame(technologies, columns=column_names)
print(df)

# Add column names while reading a CSV file
df = pd.read_csv('courses.csv', names=column_names)

# Create DataFrame with out column labels
df=pd.DataFrame([ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ])

# Assign column names to existing DataFrame
column_names=["Courses","Fee",'Duration']
df.columns = column_names
print(df)

Frequently Asked Questions on Pandas Add Column Names to DataFrame

How can I add column names to a Pandas DataFrame while creating it from a dictionary?

To add column names to a Pandas DataFrame while creating it from a dictionary, you can use the columns parameter of the pd.DataFrame constructor.

What if I already have a DataFrame and want to change the column names?

If you already have a DataFrame and want to change the column names, you can directly assign a new list of column names to the columns attribute of the DataFrame.

How can I add a new column to an existing DataFrame with a specific name and values?

To add a new column to an existing DataFrame with a specific name and values, you can use the assign method. For example, the assign method is used to add a new column named ‘Column3’ with the specified values to the existing DataFrame. The resulting DataFrame will have the original columns along with the new one.

When reading a CSV file, how can I specify column names if the file doesn’t have a header?

If your CSV file doesn’t have a header, and you want to specify column names while reading it into a Pandas DataFrame, you can use the names parameter of the read_csv function.

If my CSV file has a header, how can I read it and use those names as column names?

If your CSV file has a header (i.e., the first row contains column names), you can simply use the read_csv function without specifying the names parameter. The read_csv function automatically uses the first row of the CSV file as column names.

Conclusion

In this article, you have learned to assign column names to pandas DataFrame, while creating, when reading a CSV and to an existing DataFrame.

Happy Learning!!

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium