![]() |
VOOZH | about |
In data analysis, it is often necessary to add a new column to a DataFrame based on specific conditions. This article demonstrates multiple methods to create a column in Pandas depending on the values of another column.
Given a DataFrame containing details of a cultural event, add a column called Price which contains the ticket price for each day based on the type of event.
Output
Date Event
0 11/8/2011 Music
1 11/9/2011 Poetry
2 11/10/2011 Music
3 11/11/2011 Comedy
4 11/12/2011 Poetry
Output :
Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 800
4 11/12/2011 Poetry 800
Explanation:
Output
Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 1200
4 11/12/2011 Poetry 800
Explanation:
Output
Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 1200
4 11/12/2011 Poetry 800
Explanation:
Output
Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 800
4 11/12/2011 Poetry 800
Explanation:
Output
Date Event Category
0 11/8/2011 Music Entertainment
1 11/9/2011 Poetry Literature
2 11/10/2011 Music Entertainment
3 11/11/2011 Comedy Entertainment
4 11/12/2011 Poetry Literature
Explanation:
Output
Date Event Genre
0 11/8/2011 Music Rock
1 11/9/2011 Poetry Literary
2 11/10/2011 Music Rock
3 11/11/2011 Comedy Other
4 11/12/2011 Poetry Literary
Explanation:
Output
Date Event Priority
0 11/8/2011 Music High
1 11/9/2011 Poetry Low
2 11/10/2011 Music High
3 11/11/2011 Comedy Low
4 11/12/2011 Poetry Low
Explanation: