![]() |
VOOZH | about |
In this article, we’ll explore different ways to create a new column in a Pandas DataFrame based on existing columns. This is a common task in data analysis when you need to transform or categorize your data.
Output
Date Event Cost
0 10/2/2011 Music 10000
1 11/2/2011 Poetry 5000
2 12/2/2011 Theatre 15000
3 13/2/2011 Comedy 2000
apply() function allows us to apply a custom function to each row or column. Here, we create a new column Discounted_Price by applying a 10% discount on the Cost column.
Output
Date Event Cost Discounted_Price
0 10/2/2011 Music 10000 9000.0
1 11/2/2011 Poetry 5000 4500.0
2 12/2/2011 Theatre 15000 13500.0
3 13/2/2011 Comedy 2000 1800.0
Explanation:
Another simpler approach to create a new column is to perform an element-wise operation on an existing column. Here, we will directly apply the discount calculation to the Cost column.
Output
Date Event Cost Discounted_Price
0 10/2/2011 Music 10000 9000.0
1 11/2/2011 Poetry 5000 4500.0
2 12/2/2011 Theatre 15000 13500.0
3 13/2/2011 Comedy 2000 1800.0
Explanation:
map() function is useful when you want to map one set of values to another. In this example, we’ll create a new column called salary_stats based on the salary column by using a mapping function.
Output
Date Event Cost Cost_Category
0 10/2/2011 Music 10000 Medium
1 11/2/2011 Poetry 5000 Medium
2 12/2/2011 Theatre 15000 High
3 13/2/2011 Comedy 2000 Low
Explanation: