The Wayback Machine - https://web.archive.org/web/20240707065856/https://www.geeksforgeeks.org/add-column-names-to-dataframe-in-pandas/
Open In App

Add column names to dataframe in Pandas

Last Updated : 24 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to add column names to a dataframe. Let us how to add names to DataFrame columns in Pandas.

Add Column Names to Pandas Dataframe

Below are the steps and methods by which we can add column names in the Pandas dataframe in Python:

Creating the DataFrame

Let’s first create an example DataFrame for demonstration reasons before moving on to adding column names. There are several ways in Pandas to add column names to your DataFrame:

python3




# importing the pandas library
import pandas as pd
 
# creating lists
l1 =["Amar", "Barsha", "Carlos", "Tanmay", "Misbah"]
l2 =["Alpha", "Bravo", "Charlie", "Tango", "Mike"]
l3 =[23, 25, 22, 27, 29]
l4 =[69, 54, 73, 70, 74]
 
# creating the DataFrame
team = pd.DataFrame(list(zip(l1, l2, l3, l4)))
 
# displaying the DataFrame
print(team)


Output

0        1   2   3
0 Amar Alpha 23 69
1 Barsha Bravo 25 54
2 Carlos Charlie 22 73
3 Tanmay Tango 27 70
4 Misbah Mike 29 74

Here we can see that the columns in the DataFrame are unnamed.

Adding column name to the DataFrame

We can add columns to an existing DataFrame using its columns attribute.

python3




# adding column name to the respective columns
team.columns =['Name', 'Code', 'Age', 'Weight']
 
# displaying the DataFrame
print(team)


 Output

 Name     Code  Age  Weight
0 Amar Alpha 23 69
1 Barsha Bravo 25 54
2 Carlos Charlie 22 73
3 Tanmay Tango 27 70
4 Misbah Mike 29 74

Now the DataFrame has column names.

Adding Column Name using dataframe()

We can add column name by using giving a parameter inside the dataframe() function.

Python3




column_name = ['Name', 'Code', 'Age', 'Weight']
team = pd.DataFrame(list(zip(l1, l2, l3, l4)), columns=column_name)
print(team)


Output

 Name     Code  Age  Weight 
0 Amar Alpha 23 69
1 Barsha Bravo 25 54
2 Carlos Charlie 22 73
3 Tanmay Tango 27 70
4 Misbah Mike 29 74

Renaming Column Name of a DataFrame

We can rename the columns of a DataFrame by using the rename() function.

python3




# reanming the DataFrame columns
team.rename(columns = {'Code':'Code-Name',
                       'Weight':'Weight in kgs'},
            inplace = True)
 
# displaying the DataFrame
print(team)


Output

Name Code-Name  Age  Weight in kgs
0 Amar Alpha 23 69
1 Barsha Bravo 25 54
2 Carlos Charlie 22 73
3 Tanmay Tango 27 70
4 Misbah Mike 29 74

We can see the names of the columns have been changed.



Similar Reads

Change column names and row indexes in Pandas DataFrame
Given a Pandas DataFrame, let's see how to change its column names and row indexes. About Pandas DataFramePandas DataFrame are rectangular grids which are used to store data. It is easy to visualize and work with data when stored in dataFrame. It consists of rows and columns.Each row is a measurement of some instance while column is a vector which
4 min read
How to Sort a Pandas DataFrame based on column names or row index?
Pandas dataframe.sort_index() method sorts objects by labels along the given axis. Basically, the sorting algorithm is applied to the axis labels rather than the actual data in the Dataframe and based on that the data is rearranged. Creating Pandas Dataframe Create a DataFrame object from the Python list of tuples with columns and indices, say colu
3 min read
Get column index from column name of a given Pandas DataFrame
In this article we will see how to get column index from column name of a Dataframe. We will use Dataframe.columns attribute and Index.get_loc method of pandas module together. Syntax: DataFrame.columns Return: column names index Syntax: Index.get_loc(key, method=None, tolerance=None) Return: loc : int if unique index, slice if monotonic index, els
2 min read
Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
This article demonstrates multiple examples to convert the Numpy arrays into Pandas Dataframe and to specify the index column and column headers for the data frame. Example 1: In this example, the Pandas dataframe will be generated and proper names of index column and column headers are mentioned in the function. This approach can be used when ther
4 min read
Convert given Pandas series into a dataframe with its index as another column on the dataframe
First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series(data, index, dtype, copy) Return: Series object.
1 min read
How to add column sum as new column in PySpark dataframe ?
In this article, we are going to see how to perform the addition of New columns in Pyspark dataframe by various methods. It means that we want to create a new column that will contain the sum of all values present in the given row. Now let's discuss the various methods how we add sum as new columns But first, let's create Dataframe for Demonstratio
4 min read
How to add Empty Column to Dataframe in Pandas?
In DataFrames, Empty columns are defined and represented with NaN Value(Not a Number value or undefined or unrepresentable value). There are various methods to add Empty Column to Pandas Dataframe in Python. Method 1: Add Empty Column to Dataframe using the Assignment Operator We are using the assignment operator to assign empty strings to two newl
3 min read
Add Column to Pandas DataFrame with a Default Value
The three ways to add a column to Pandas DataFrame with Default Value. Using pandas.DataFrame.assign(**kwargs)Using [] operatorUsing pandas.DataFrame.insert()Using Pandas.DataFrame.assign(**kwargs) It Assigns new columns to a DataFrame and returns a new object with all existing columns to new ones. Existing columns that are re-assigned will be over
2 min read
Add column with constant value to pandas dataframe
Prerequisite: Pandas In this article, we will learn how to add a new column with constant value to a Pandas DataFrame. Before that one must be familiar with the following concepts: Pandas DataFrame : Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular arrangement with labeled axes (rows and columns). A Data frame may
2 min read
How to add column from another DataFrame in Pandas ?
In this discussion, we will explore the process of adding a column from another data frame in Pandas. Pandas is a powerful data manipulation library for Python, offering versatile tools for handling and analyzing structured data. Add column from another DataFrame in Pandas There are various ways to add a column from another DataFrame in Pandas. Her
6 min read
Practice Tags :
three90RightbarBannerImg