How to Create Conditional Columns in Pandas | IF ELSE Condition in Pandas Data Frame
ฝัง
- เผยแพร่เมื่อ 28 พ.ย. 2022
- 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:
• All About SQL Aggregat...
Most Asked Join Based Interview Question:
• Most Asked SQL JOIN ba...
Solving 4 Trick SQL problems:
• Solving 4 Tricky SQL P...
Data Analyst Spotify Case Study:
• Data Analyst Spotify C...
Top 10 SQL interview Questions:
• Top 10 SQL interview Q...
Interview Question based on FULL OUTER JOIN:
• SQL Interview Question...
Playlist to master SQL :
• Complex SQL Questions ...
Rank, Dense_Rank and Row_Number:
• RANK, DENSE_RANK, ROW_...
#python #pandas #dataanalytics - วิทยาศาสตร์และเทคโนโลยี
I dont know why Ankit is soo underrated on TH-cam. He got skills to make difficult topic super easy. Thanks for doing this but you are tooo good with skills and knowledge #Respect !
Thank you 😊
Great video Ankit
Please continue the series just like SQL series.
Thanks in advance
thanks. very useful
Please post regular videos in This playlist. This is great actually!
Sure
Great experience
Great video as usual sir💯
Thank you 😊
Thank you very much for the clear explanation.😊
Glad it was helpful!
Thank you for this amazing video. kudos
Glad you enjoyed it!
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
That you can simply create using df['len']= df.name.str.len()
@@ankitbansal6
I said i will use the apply() method and they were not convinced.
What is the difference in apply() method and directly using Len() ?
@@sashikanthpalleti5757 apply function is a loop itself. Direct assignment is a dataframe operation and faster.
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.
👌grt content
Thank you 😊
great video sir ...need more pandas video
Sure
good
amazing ... its helpful..please upload more vids on pandas
Sure
@@ankitbansal6 Hi bro, this is the playlist im looking for. Very helpful. Kindly create more content on pandas and numpy. Cheers.
Please create a video for scenarios where Lambda function can be used.
Ok
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)
Apply runs a loop
how i can compare 2 columns using if else in order to classify? Ex: [Column A > Column B: 'Good Customer' else 'Bad Customer']
were you able to find a solution for it?
Solution using numpy's # np.select(conditions, values)
import numpy as np
conditions = [(df['profit']250) & (df['profit']500)]
values=['low','medium','high']
df['profit_category'] = np.select(conditions,values)
df