Tuesday 10 October 2023

How to Create a New Column from an Existing Column in a Pandas DataFrame?

In this post, I am going to explain how to create a new column by applying some transformations to the existing column.

 

To demonstrate the examples, I am using below dataset.

       Name  Age       City
0   Krishna   34  Bangalore
1       Ram   25    Chennai
2      Joel   29  Hyderabad
3      Gopi   41  Hyderabad
4  Jitendra   52  Bangalore
5       Raj   23    Chennai

 

Example 1: Add new column Age_plus_10 to the DataSet

df['Age_plus_10'] = df['Age'] + 10

 

Above statement create new column ‘Age_plus_10’ by adding 10 to the existing column Age.

 

Example 2: Add new column Age_minus_10 to the DataSet

df['Age_minus_10'] = df['Age'].sub(10)

 

Above statement create new column ‘Age_minus_10’ by subtracting 10 to the existing column Age.

 

Example 3: Add new columns City_in_upper_case by converting the City to uppercase.

df['City_in_upper_case'] = df['City'].str.upper()

 

Find the below working application.

 

new_column_from_existing_column.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel', 'Gopi', 'Jitendra', "Raj"],
        'Age': [34, 25, 29, 41, 52, 23],
        'City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai']}

df = pd.DataFrame(data)
print(df)

print('\nAdd new column Age_plus_10')
df['Age_plus_10'] = df['Age'] + 10
print(df)

print('\nAdd new column Age_minus_10')
df['Age_minus_10'] = df['Age'].sub(10)
print(df)

print('\nAdd new column City_in_upper_case')
df['City_in_upper_case'] = df['City'].str.upper()
print(df)

 

Output

       Name  Age       City
0   Krishna   34  Bangalore
1       Ram   25    Chennai
2      Joel   29  Hyderabad
3      Gopi   41  Hyderabad
4  Jitendra   52  Bangalore
5       Raj   23    Chennai

Add new column Age_plus_10
       Name  Age       City  Age_plus_10
0   Krishna   34  Bangalore           44
1       Ram   25    Chennai           35
2      Joel   29  Hyderabad           39
3      Gopi   41  Hyderabad           51
4  Jitendra   52  Bangalore           62
5       Raj   23    Chennai           33

Add new column Age_minus_10
       Name  Age       City  Age_plus_10  Age_minus_10
0   Krishna   34  Bangalore           44            24
1       Ram   25    Chennai           35            15
2      Joel   29  Hyderabad           39            19
3      Gopi   41  Hyderabad           51            31
4  Jitendra   52  Bangalore           62            42
5       Raj   23    Chennai           33            13

Add new column City_in_upper_case
       Name  Age       City  Age_plus_10  Age_minus_10 City_in_upper_case
0   Krishna   34  Bangalore           44            24          BANGALORE
1       Ram   25    Chennai           35            15            CHENNAI
2      Joel   29  Hyderabad           39            19          HYDERABAD
3      Gopi   41  Hyderabad           51            31          HYDERABAD
4  Jitendra   52  Bangalore           62            42          BANGALORE
5       Raj   23    Chennai           33            13            CHENNAI

 


Previous                                                 Next                                                 Home

No comments:

Post a Comment