• Post author:
  • Post category:Pandas
  • Post last modified:May 20, 2024
  • Reading time:11 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 lacking column names, requiring you to add them after reading the CSV data into a DataFrame.

Advertisements

In this article, I will explain adding or assigning column names to a DataFrame through various examples. The column names on DataFrame are used to recognize what type of data each column holds.

Quick Examples of Add Column Names

Following are quick examples of adding/assigning or setting column labels to Pandas 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

Add Column Names to DataFrame

When manually creating a pandas DataFrame from a data object, you have the option to add column names. In order to create a DataFrame, utilize the DataFrame constructor, which includes a columns parameter for assigning names. This parameter accepts a list as its value, ensuring that the number of values in the list matches the number of columns in the 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

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)

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

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)

FAQs on 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.

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 conclusion, we’ve explored various methods of adding column names to pandas DataFrames. Whether it’s during DataFrame creation, when reading from a CSV file, or for an existing DataFrame, pandas provide flexible options for organizing and manipulating your data efficiently.

Happy Learning!!