DEV Community

Cover image for How create a new field based on other numerical fields in a Pandas Dataframe
Jesus Blazquez
Jesus Blazquez

Posted on

How create a new field based on other numerical fields in a Pandas Dataframe

To create a new field based on other numerical fields in a pandas DataFrame, you can use the pandas.DataFrame.assign() method. This method allows you to specify a new column name and the values for the column, which can be derived from one or more existing columns in the DataFrame.

For example, suppose you have a DataFrame with columns A, B, and C, and you want to create a new column called D that is equal to the sum of columns A and B. You can use the following code to do that:

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

df = df.assign(D = df['A'] + df['B'])

print(df)
Enter fullscreen mode Exit fullscreen mode

This code will create a new column D in the DataFrame with the values from columns A and B added together. The resulting DataFrame will look like this:

   A  B  C  D
0  1  4  7  5
1  2  5  8  7
2  3  6  9  9
Enter fullscreen mode Exit fullscreen mode

Alternatively, you could also use the pandas.DataFrame.apply() method to apply a custom function to each row of the DataFrame, and create the new column based on the output of that function. For example, the following code would accomplish the same task as the code above:

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

def sum_columns(row):
    return row['A'] + row['B']

df = df.assign(D = df.apply(sum_columns, axis=1))

print(df)
Enter fullscreen mode Exit fullscreen mode

This code will also create a new column D in the DataFrame with the values from columns A and B added together. The resulting DataFrame will be the same as the one shown above.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay