Pandas Conditional Columns: Set Pandas Conditional Column Based on Values of Another Column

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.ค. 2024
  • In this video, you'll learn how to set a Pandas column values based on values of another column. You'll learn how to set values based on a single condition, multiple conditions, and emulating a VLOOKUP in Python.
    To see the written tutorial and all the sample code, check out my website here: datagy.io/pandas-conditional-...
    0:00 Introduction
    0:55 Using loc to Set Conditional Values
    3:50 Using Numpy Select
    6:23 Pandas Map a Dictionary
    9:00 Pandas Apply a Custom Function
    12:20 Conclusion
    Learn Python programming the right way!
    ----------------------------------------------------------
    ✅ Check out my eBook to get started with Python for Data Science: gumroad.com/l/introtopythonfo...
    ✅ Follow me on other platforms:
    Website: datagy.io
    Twitter: / datagyio
    Mailing List: mailchi.mp/f6a58f2d73d0/datag... (and get a FREE Pandas tip and tricks book and a FREE Guide to SQL)

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

  • @guidysoll
    @guidysoll ปีที่แล้ว +6

    I spend two days looking for a solution for a conditional problem. This video was exactly what I needed. Thank you very much!!!

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

      Thanks so much for the feedback!!

  • @19AKS58
    @19AKS58 2 หลายเดือนก่อน

    There are SO many, quite frankly TOO many instructional videos, and they are tedious, too long, and unclear, but YOURS is EXACTLY on point

    • @datagy
      @datagy  2 หลายเดือนก่อน

      Thank you so much! I really appreciate it.

  • @hashemrashidi1277
    @hashemrashidi1277 2 หลายเดือนก่อน

    Guys he is the best and simplest , best one seriously , just trust and watch till the end .

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

    I've been looking for a video to help with my code for days. Your vid was by far the best and only one that added value. Well done!

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

      Thanks so much, Andrew!

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

    The numpy option was exactly what I needed and worked great. Thank you, also for the other videos. Keep up the good work…

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks so much for your feedback! 😊

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

      For me too

  • @Zenoandturtle
    @Zenoandturtle 8 หลายเดือนก่อน +1

    Great presntation. I am building a trading bot using algorythm that deals with conditional columns (candlestick lengths of previous columbs) This is the video I was looking for. Thanks for the vid. Very clear and informative. Exactly what I was looking for.

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

    This is an excellent Tutorial !!! . Beautifully explained, and you went over issues I have been struggling with. Thank you so much for taking the time to make this video

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

      Thanks so much Ali!

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

    Excellent video! Thanks!!!

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

    Thus video is very helpful and exactly what I needed! Thank you for the multiple examples with different scenarios!

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thank you so much!

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

    Thank you bro that what i was looking for my problem to be solved. Great!!!!

  • @wearerunnersw.a.r5613
    @wearerunnersw.a.r5613 2 ปีที่แล้ว +2

    great explanation, used the numpy method. works brilliantly

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

      Thanks so much!

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

    Excellent video! Just what I need to solve a problem. Thank you!

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

      Thank you!! I appreciate the comment!

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

    This was a clear and concise explanation that helped me a lot. Thank you for all that you do!

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

      Thank you SO much David! That was very generous of you! I’m so glad you liked the video!

    • @Zenoandturtle
      @Zenoandturtle 8 หลายเดือนก่อน +1

      One of the best I had seen.

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

    your video is so good, thank u so much I was looking for this everywhere :)

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thank you! That means a lot!

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

    This is really awesome. Thankyou for this tutorial.

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

      Thanks so much!

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

    Oh man -- I've been trying to figure out more efficient ways of defining bins for a df. While I'm fairly new to Python, I've coded (in SAS) for 15 years, but that experience is only helpful in knowing how I would solve a coding problem -- in SAS.
    The numpy approach you mentioned is very simple and intuitive -- your tips are going to save me so much time! Thanks for making this video!!!

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

      Thank you so much for this comment Andy! Made my day!

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

    Excellent presentation… you really helped me understand map & apply methods

    • @datagy
      @datagy  2 ปีที่แล้ว

      I'm so glad it was helpful!

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

    Works like a charm! Many thanks dude :)

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

      Thanks Andrés!

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

    heey!! this video provides everything I need for a project I have in mind..

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

    thanks a lot, you saved me a lot of time and effort

    • @datagy
      @datagy  2 ปีที่แล้ว

      I’m so glad!!

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

    Thanks for clear explanation🎉

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

      Thanks Zarir!

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

    Excellent. Thanks for sharing your knowledge.

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks Zac!

  • @Planetknowledge555
    @Planetknowledge555 2 หลายเดือนก่อน

    Excellent

  • @Sandeepkumar-dm2bp
    @Sandeepkumar-dm2bp 2 ปีที่แล้ว +1

    Excellent tutorial nice explanations, concise, covered many related concepts, various ways to do,

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thank you so much!

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

    Thanks for the useful lesson.

    • @datagy
      @datagy  2 ปีที่แล้ว

      I'm glad it was helpful!

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

    That's helpful beginner stuff, Thanks

    • @datagy
      @datagy  2 ปีที่แล้ว

      I’m glad it was helpful!

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

    Thanks man!

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thanks for your comment!

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

    Great video!

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

      Thanks Kenneth!

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

    wow bro. that is very helpful. from indonesia, thank you..

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

      Thanks Firdi!!

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

    Great explanation

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thank you!

  • @CX004
    @CX004 2 ปีที่แล้ว

    Awesome! Could you show an example taking in 2 parameters instead? Say taking Age and Gender... How do we achieve that using the function-apply approach?

  • @Mmmkay..
    @Mmmkay.. 2 ปีที่แล้ว +1

    great video !

    • @datagy
      @datagy  2 ปีที่แล้ว

      Thank you!!

  • @zainhana2968
    @zainhana2968 2 ปีที่แล้ว

    Can u give me an example if using nested conditional? Because when I have tried my nested conditional with function and loop, the value in new coloumn is nan

  • @maxons.e4643
    @maxons.e4643 ปีที่แล้ว

    This is great. But I need to do this for multiple columns. How do add values from another column to the new column without it affecting the existing values (except where there is null?

  • @munivoltarc
    @munivoltarc 6 หลายเดือนก่อน

    Hi, you are doing awesomely work, explaining subject so easily to all, wondaful work, keep it up, could you explain OHLCV data, apply, map functions, user defined functions to to find the trend on the High, Low columns, to get buy signals and sell signals by not using any price lagging indicators.

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

    Superb tutorial. Keep it up

  • @victormadoya1354
    @victormadoya1354 5 หลายเดือนก่อน

    HI, what if you want to create a similar function with takes 2 values? such that the 2 values will be 2 columns rather than using one column. The def function to take 2 columns instead of one, then use apply function

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

    What about if i have a column that contains several words (on each row) and i need to create a new column with 1s/0s IF ... my records contain a specific word AND (at same time) doesn't contain (another) specific word. I want to flag those records

  • @ajibademotunrayo8453
    @ajibademotunrayo8453 2 ปีที่แล้ว

    Hi
    how do you replace zero values in a column based on multiple conditions

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

    I have 4 lac records. And I have to assign url based unique ID.. how I can do that?

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

    Hi Thank you. I constantly receive this error, could you please help?
    'NAType' object has no attribute 'str'

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

      def SAPID (TotalData1):
      if (TotalData1['Type_SubCat']=='Billing'):
      return TotalData1['Sales Document']
      elif (TotalData1['User Name']=='ANSAZIY'):
      return 'OPENNING BALANCE'
      elif (TotalData1['Assignment'].str[0]=='4'):
      return TotalData1['Assignment'].str[10]
      elif (TotalData1['Assignment'].str[0]=='L'):
      return TotalData1['Assignment'].str[8]
      else:
      return TotalData1['Assignment']

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

      It’s likely that you have missing data in your column - you can fill those first before applying the conditional column.

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

    Hi, for example I have a table, which I've got by left outer join:
    person - vehicle
    dad - car
    dad - motorcycle
    dad - bicycle
    mom - car
    mom - bicycle
    son - None/NA/NaN/NaT
    How to group by person and count with condition (car and motorcycle)?
    When I use for example:
    df = df.groupby(['person'])['vehicle'].apply(lambda x: x[x == 'car'].count())
    But I can't use a list in condition lambda x: x[x in ['car']], pandas says:
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

  • @maxons.e4643
    @maxons.e4643 ปีที่แล้ว

    I will be very very grateful if you could reply this. i am on a hot seat.

  • @Gaming_Hub47
    @Gaming_Hub47 2 ปีที่แล้ว

    h=1
    temp=h
    height=[]
    for i in range(60):
    if a[i]>b[i]:
    h=2
    temp=h
    height.append(h)
    elif a[i]

    • @datagy
      @datagy  2 ปีที่แล้ว

      Hey Abhishek, sorry, I’m not sure what you’re hoping to accomplish. Could you elaborate?

    • @Gaming_Hub47
      @Gaming_Hub47 2 ปีที่แล้ว

      @@datagy we have two lists here, list a[] and b[] which contains some values, and the length of each lists is 60 thats why the for loop range is 60, then in the first 'if' condition if the 'i' value of the list a[] is greater then 'i' value of list b[] then the value of h will be 2 and we will append the value of h in the height[] list and similar condition is for second elif condition too but if both the condition fails then the value of h will be whatever the previous value of h, and i used temp variable to accomplish that....so how i will change all these scenarios according to the data frame......where list a and b are now two columns of the dataframe and instead of appending the values of h into a list i want to create a new column in the dataframe which will store it.....i hope that now it clears all the condition ......please help if possible..
      it is like how can we compare 2 columns of a dataframe and if possible then how can we assign h the previous value when both the condition fails

    • @datagy
      @datagy  2 ปีที่แล้ว

      @@Gaming_Hub47 Thanks for the explanation! I'm struggling to picture the flow of the data. Can you include the list a and b for me to better understand? Happy to try and help!

    • @Gaming_Hub47
      @Gaming_Hub47 2 ปีที่แล้ว

      @@datagy really sry i missed the point that there is 3 list ....now i corrected the code, there is a list c also ....this problem is so confusing.....i really appreciate that you try to solve my issue........ list a is=[88.85, 88.645, 88.985, 88.775, 88.745, 89.165, 89.495, 88.905, 88.9, 89.015, 89.295, 89.325, 89.375, 89.265, 89.415, 89.465, 89.54, 89.54, 89.24, 89.16, 89.155, 89.155, 89.095, 89.22, 89.2, 89.245, 89.365, 88.935, 89.33, 89.325, 89.185, 88.885, 88.645, 88.65, 88.215, 88.18, 88.575, 88.595, 88.535, 88.585, 88.52, 88.765, 88.97, 88.965, 88.91, 88.69, 88.99, 89.055, 88.75, 88.63, 88.625, 88.39, 88.415, 88.33, 88.76, 88.585, 88.56, 88.85, 88.88, 89.03, 89.575, 89.56, 89.7, 90.395, 90.335, 89.875, 88.845, 88.94, 88.77, 88.61, 88.845, 88.47, 88.615, 88.7, 88.725, 88.9, 88.455, 88.48, 88.655, 88.49, 88.37, 88.535, 88.45, 88.5, 88.885, 88.72, 88.815, 88.905, 88.775, 88.88, 89.14, 89.425, 89.315, 89.005, 89.4, 89.405, 89.38, 89.17, 89.465, 89.19, 89.11, 89.245, 89.28, 89.26, 89.02, 88.865, 88.98, 88.93, 88.87, 88.67, 88.75, 88.895, 88.9, 88.915, 89.15, 89.285, 89.285, 89.05, 88.905, 89.06]
      list b is= [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 89.618, 89.668, 89.672, 89.619, 89.523, 89.413, 89.307, 89.226, 89.192, 89.199, 89.241, 89.291, 89.348, 89.384, 89.376, 89.293, 89.16, 88.997, 88.773, 88.609, 88.502, 88.471, 88.498, 88.533, 88.604, 88.709, 88.821, 88.925, 89.006, 89.057, 89.096, 89.122, 89.092, 89.024, 88.935, 88.798, 88.675, 88.608, 88.589, 88.597, 88.65, 88.738, 88.854, 89.111, 89.403, 89.663, 89.975, 90.277, 90.515, 90.62, 90.475, 90.184, 89.776, 89.409, 89.12, 88.852, 88.675, 88.581, 88.594, 88.653, 88.673, 88.694, 88.697, 88.682, 88.663, 88.63, 88.6, 88.659, 88.748, 88.851, 88.956, 89.04, 89.086, 89.164, 89.254, 89.352, 89.427, 89.503, 89.555, 89.59, 89.591, 89.572, 89.555, 89.507, 89.438, 89.371, 89.331, 89.302, 89.242, 89.178, 89.123, 89.07, 89.013, 88.931, 88.87, 88.837, 88.839, 88.892, 88.999, 89.124, 89.238, 89.293, 89.304]
      l [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 89.442, 89.467, 89.441, 89.378, 89.281, 89.179, 89.09, 89.025, 89.008, 89.025, 89.024, 89.004, 89.023, 89.053, 89.05, 88.984, 88.867, 88.667, 88.385, 88.141, 88.009, 87.993, 88.06, 88.162, 88.275, 88.414, 88.563, 88.703, 88.792, 88.833, 88.871, 88.878, 88.837, 88.765, 88.654, 88.532, 88.411, 88.312, 88.262, 88.264, 88.312, 88.427, 88.563, 88.727, 88.939, 89.156, 89.393, 89.688, 89.914, 89.914, 89.713, 89.408, 89.009, 88.664, 88.378, 88.14, 88.032, 88.02, 88.098, 88.203, 88.281, 88.329, 88.371, 88.378, 88.348, 88.312, 88.29, 88.307, 88.375, 88.46, 88.557, 88.655, 88.732, 88.784, 88.873, 88.983, 89.059, 89.101, 89.162, 89.232, 89.269, 89.266, 89.242, 89.187, 89.126, 89.044, 89.003, 88.968, 88.93, 88.891, 88.843, 88.81, 88.758, 88.693, 88.634, 88.598, 88.611, 88.66, 88.759, 88.898, 89.007, 89.069, 89.067]

    • @Gaming_Hub47
      @Gaming_Hub47 2 ปีที่แล้ว

      so these are the 3 lists .....please now try the code with all the values in ....maybe now it is somewhat easy

  • @juliobarber4206
    @juliobarber4206 2 ปีที่แล้ว

    😃 P R O M O S M!