How to replace an entire column on pandas dataframe?

Given a Pandas DataFrame, we have to replace an entire column.
Submitted by Pranit Sharma, on August 07, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

Sometimes, we want to replace an entire column on a Pandas DataFrame with another column taken from another DataFrame or we want to replace the entire column with all the values of another column of the same DataFrame.

Replace an entire column on pandas dataframe

If we want to replace with the column of another column, it is obvious that we have two different DataFrames, but if we want to replace the column with the column of the same DataFrame then we have two cases, first, we can replace the entire column with a column which already exists, second, first we have to create a new column for this DataFrame and then we assign all of its values to the old column that we want to replace.

For this purpose, we can simply replace the column by accessing the column using square brackets with DataFrame like df[col]. After doing this, we will copy all the contents of another column by using the assignment operator.

Let us understand with the help of an example,

Python program to replace an entire column on pandas dataframe

# Importing pandas package
import pandas as pd

# Creating two dictionary
d1 = {
    'Lower_Case':['a','b','c','d','e','f','g','h','i','j'],
    'Upper_Case':[1,2,3,4,5,6,7,8,9,10]
}

d2 = {
    'Upper_Case':['A','B','C','D','E','F','G','H','I','J']
    
}

# Creating two separate DataFrames
df1 = pd.DataFrame(d1)
df2 = pd.DataFrame(d2)

# Display DataFrames
print("DataFrame1:\n",df1,"\n")
print("DataFrame2:\n",df2,"\n")

# Assigning column of df2 to a new column of df1
df1['Upper_Case'] = df2['Upper_Case']

# Display modified DataFrame 1
print("Modified DataFrame 1:\n",df1)

Output

DataFrame1:
   Lower_Case  Upper_Case
0          a           1
1          b           2
2          c           3
3          d           4
4          e           5
5          f           6
6          g           7
7          h           8
8          i           9
9          j          10 

DataFrame2:
   Upper_Case
0          A
1          B
2          C
3          D
4          E
5          F
6          G
7          H
8          I
9          J 

Modified DataFrame 1:
   Lower_Case Upper_Case
0          a          A
1          b          B
2          c          C
3          d          D
4          e          E
5          f          F
6          g          G
7          h          H
8          i          I
9          j          J

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.