Pandas How To Uncategorized How to add column based on other columns in Pandas

How to add column based on other columns in Pandas

Adding a new column to a Pandas dataframe based on the values of other columns is a common task in data analysis. Here’s how you can add a new column based on other columns in Pandas:

Assuming you have a dataframe df with columns col1, col2, and col3, and you want to create a new column new_col based on the values of col1 and col2, you can do the following:

df['new_col'] = df['col1'] + df['col2']

In the above example, we are creating a new column new_col by adding the values of col1 and col2 using the + operator. You can replace this with any other operation you want, such as subtraction, multiplication, or division.

You can also use functions to perform more complex operations on the columns. For example, if you want to create a new column new_col based on a conditional statement using values from col1 and col2, you can do the following:

import numpy as np

df['new_col'] = np.where(df['col1'] > df['col2'], 'A', 'B')

In the above example, we are using the np.where() function to create a new column new_col based on a conditional statement. If the value in col1 is greater than the value in col2, we set the value of new_col to A, otherwise we set it to B.

These are just some examples of how you can add a new column based on other columns in Pandas. You can perform any operation or use any function to create the new column based on the values of other columns.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post