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
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 34

  • @mihirsamant9352
    @mihirsamant9352 ปีที่แล้ว +2

    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 !

  • @Mayank-jw9yy
    @Mayank-jw9yy 3 หลายเดือนก่อน

    Great video Ankit

  • @ratneshraj4653
    @ratneshraj4653 ปีที่แล้ว

    Please continue the series just like SQL series.
    Thanks in advance

  • @ssteo4136
    @ssteo4136 8 หลายเดือนก่อน

    thanks. very useful

  • @pratibhamishra869
    @pratibhamishra869 ปีที่แล้ว +1

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

  • @sudiipkumarbasu4194
    @sudiipkumarbasu4194 ปีที่แล้ว

    Great experience

  • @avi8016
    @avi8016 ปีที่แล้ว

    Great video as usual sir💯

  • @hameedkaryab
    @hameedkaryab 7 หลายเดือนก่อน

    Thank you very much for the clear explanation.😊

    • @ankitbansal6
      @ankitbansal6  7 หลายเดือนก่อน

      Glad it was helpful!

  • @divsp777
    @divsp777 ปีที่แล้ว

    Thank you for this amazing video. kudos

  • @sashikanthpalleti5757
    @sashikanthpalleti5757 ปีที่แล้ว +5

    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

    • @ankitbansal6
      @ankitbansal6  ปีที่แล้ว +2

      That you can simply create using df['len']= df.name.str.len()

    • @sashikanthpalleti5757
      @sashikanthpalleti5757 ปีที่แล้ว +1

      @@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() ?

    • @ankitbansal6
      @ankitbansal6  ปีที่แล้ว +3

      @@sashikanthpalleti5757 apply function is a loop itself. Direct assignment is a dataframe operation and faster.

  • @sonal008
    @sonal008 ปีที่แล้ว

    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.

  • @ektakumari4496
    @ektakumari4496 ปีที่แล้ว

    👌grt content

  • @rishav144
    @rishav144 ปีที่แล้ว

    great video sir ...need more pandas video

  • @user-zf3it3vw2e
    @user-zf3it3vw2e 9 หลายเดือนก่อน

    good

  • @shravank1147
    @shravank1147 ปีที่แล้ว

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

    • @ankitbansal6
      @ankitbansal6  ปีที่แล้ว

      Sure

    • @salmankhan-vq7pc
      @salmankhan-vq7pc ปีที่แล้ว

      @@ankitbansal6 Hi bro, this is the playlist im looking for. Very helpful. Kindly create more content on pandas and numpy. Cheers.

  • @khushishrivastava331
    @khushishrivastava331 ปีที่แล้ว +2

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

  • @parveen8122
    @parveen8122 ปีที่แล้ว +1

    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)

  • @LONGTRINH-ut4nt
    @LONGTRINH-ut4nt ปีที่แล้ว

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

    • @SheikhSirajumMonir
      @SheikhSirajumMonir 7 หลายเดือนก่อน

      were you able to find a solution for it?

  • @arunshaw1285
    @arunshaw1285 ปีที่แล้ว

    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