📜  pandas create column if equals - Python (1)

📅  最后修改于: 2023-12-03 15:18:13.535000             🧑  作者: Mango

Pandas Create Column if Equals - Python

Pandas is a powerful data analysis library in Python. It makes it easy to work with data by providing efficient tools for data manipulation, aggregation, and visualization.

One of the common tasks is to create a new column based on certain condition(s). In this article, we will see how we can create a column in pandas if it equals a certain value.

Create DataFrame

First, let's create a sample dataset using pandas' DataFrame. We will create a DataFrame containing the name and age of some people.

import pandas as pd

data = {
    'name': ['Tom', 'Jerry', 'Mike', 'John'],
    'age': [20, 22, 25, 30]
}

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

Output:

    name  age
0    Tom   20
1  Jerry   22
2   Mike   25
3   John   30
Create a new column based on a condition

Now, let's say we want to add a new column 'status' in the DataFrame based on the age of the people. For instance, if the age is less than 25, then the status would be 'young', otherwise 'matured'.

df['status'] = df['age'].apply(lambda x: 'young' if x < 25 else 'matured')
print(df)

Output:

    name  age   status
0    Tom   20    young
1  Jerry   22    young
2   Mike   25  matured
3   John   30  matured

We have added a new column 'status' in the DataFrame using pandas apply() function and a lambda function that checks the condition based on the age of the people.

Conclusion

In this article, we learned how to create a new column in pandas based on a certain condition using lambda function and apply() method. However, there are many other ways to achieve the same result using different pandas methods and functions.

We hope this article helped you to understand how to add a new column in pandas based on a condition.