How to Create Conditional Columns in Pandas | IF ELSE Condition in Pandas Data Frame

preview_player
Показать описание
In this video we will learn how to create new columns in pandas based on the value of other columns.

data:
order_id,product_name,category,city,sales,profit
CA-2020-152156,p1,Furniture,Bangalore,10000,500
CA-2020-138688,p2,Furniture,Bangalore,20000,400
US-2019-108966,p3,Technology,Chennai,25000,200
CA-2021-114412,p4,Office Supplies,Chennai,30000,250
CA-2020-161389,p5,Technology,Mysore,35000,800
US-2019-118983,p6,Office Supplies,Mysore,40000,700

Zero to hero(Advance) SQL Aggregation:

Most Asked Join Based Interview Question:

Solving 4 Trick SQL problems:

Data Analyst Spotify Case Study:

Top 10 SQL interview Questions:

Interview Question based on FULL OUTER JOIN:

Playlist to master SQL :

Rank, Dense_Rank and Row_Number:

#python #pandas #dataanalytics
Рекомендации по теме
Комментарии
Автор

I dont know why Ankit is soo underrated on Youtube. He got skills to make difficult topic super easy. Thanks for doing this but you are tooo good with skills and knowledge #Respect !

mihirsamant
Автор

The operation is happening row by row.
I was asked in an interview with ServiceNow how to add a new column in pandas dataframe with existing column string length without using row by row operations.
Ex:
Sno name
1 abch
2 sujit
3 pqr

Output:
Sno name length
1 abch 4
2 sujit 5
3 pqr 3

sashikanthpalleti
Автор

Please post regular videos in This playlist. This is great actually!

pratibhamishra
Автор

Please continue the series just like SQL series.

Thanks in advance

ratneshraj
Автор

Please create a video for scenarios where Lambda function can be used.

khushishrivastava
Автор

Thank you very much for the clear explanation.😊

hameedkaryab
Автор

Solution using apply function

using apply function, I think it wii be much more efficient and increases readibility.
in your case i think the same df will run twice, 1st for KN and then for TN

def match(city):
if city=="Bangalore" or city=='Mysore':
return 'Karnataka'
else:
return 'TN'
df["State"] = df["city"].apply(match)

parveen
Автор

great video sir ...need more pandas video

rishav
Автор

Thank you for this amazing video. kudos

divsp
Автор

amazing ... its helpful..please upload more vids on pandas

shravank
Автор

make more scenerios base qut on python

bodybuildingmotivation
Автор

How to do this for for bigger dataset. Like I have 354 unique product ID and I have to assign
Url to each in new column.

sonal
Автор

how i can compare 2 columns using if else in order to classify? Ex: [Column A > Column B: 'Good Customer' else 'Bad Customer']

LONGTRINH-utnt
Автор

Solution using numpy's # np.select(conditions, values)

import numpy as np

conditions = [(df['profit']<=250), ((df['profit']>250) & (df['profit']<=500)), (df['profit']>500)]
values=['low', 'medium', 'high']
df['profit_category'] = np.select(conditions, values)
df

arunshaw