How to compare columns in pandas

แชร์
ฝัง

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

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

    awesome.....keep making these informative short videos

    • @ChartExplorers
      @ChartExplorers  3 ปีที่แล้ว

      Will do! Thanks for your encouragement.

  • @studyhub3950
    @studyhub3950 3 ปีที่แล้ว

    very good
    helping and informative and short. keep it up.

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

    Hi @chart Explorers , i have one doubt.. when i used the same method shown here ..it is giving TypeError: unhashable type: 'Series' can you please tell me how to resolve this?

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

    Instead of it saying True / False, is there a way to make say any type of "string value", like high / low or full / empty...anything other than True / False

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

      This is a little cluncky
      df['boo'] = (df['col1'] < df['col2']).apply(lambda x: 'Less than' if True else 'Greater than eqaul to')
      or you could use something slightly more performant
      condlist = [df['col1'] < df['col2'],df['col1'] >= df['col2']]
      choicelist = ['Less than', 'Greater than']
      df['boo'] = np.select(condlist, choicelist)
      Let me know if you want an explanation (I won't be able to give it for the next 10 hours or so)

  • @shobhitasohal2853
    @shobhitasohal2853 3 ปีที่แล้ว

    hey. Can we use if statement to compare 2 columns in a new column & return certain value other than just true or false?

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

    unable to compare same thing with float values. getting incorrect result

  • @vinayakchikkorde8151
    @vinayakchikkorde8151 3 ปีที่แล้ว

    I have the source file and target file. so in that, I have to compare 140 columns and show the result if it matches or not. for example, there is a column as Country1 in source and in target as Country2. to compare that i will use if(source['country1]==target['country2])return True else return false. to compare 140+ columns it will take time to compare 140 columns. so how can I solve this?

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

    how to compare one column with the other 16 columns? These 16 columns are not next to each other.

  • @tanliz765
    @tanliz765 3 ปีที่แล้ว

    Thank you!

  • @hellofrombd1960
    @hellofrombd1960 4 ปีที่แล้ว

    How
    0
    5
    5
    5
    5
    5
    Comes???? Don't understand. If False means 0 then df['boo']+1 would be 0+1=1
    Then 1+1=2
    2
    2
    2

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

      Good catch. This was an editing mistake, I had provided more examples in my original explanation, but edited them out. If you notice at 1:46 seconds we get the expected answer (2) and then I cut out my other examples in editing. I'll see what I can do to fix this. Thanks.

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

    Comapare two colums get the records from one column..EXAMPLE A column values == Bcolummn values same print(Common values )😭😭

  • @miss_tech
    @miss_tech 3 ปีที่แล้ว

    ValueError: Can only compare identically-labeled Series objects

  • @bitwamet
    @bitwamet 3 ปีที่แล้ว

    How to add multiple condition

  • @sanahnahk7312
    @sanahnahk7312 3 ปีที่แล้ว

    thanks !

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

    How to code for how many trues r there

    • @ChartExplorers
      @ChartExplorers  3 ปีที่แล้ว

      Do you want to count the number of True values in a column? df['col_name'].sum()

    • @BhanuLekha
      @BhanuLekha 3 ปีที่แล้ว

      @@ChartExplorers s I need no.of true values in the column after comparing .thank u

    • @ChartExplorers
      @ChartExplorers  3 ปีที่แล้ว

      @@BhanuLekha A neat feature of boolean values (True and False) is that they are considered as 1's and 0's. So you can use mathematical operations on them just like a floats or ints.
      If all you need is the sum you can :
      (df['Col_1'] > df['Col_2']).sum()
      Or you could create a new column (like in the video) and then sum the column.
      df['new_col'] = df['Col_1'] > df['Col_2']
      df['new_col'].sum()
      This will return all the number of true values. If you wanted the number of False values
      len(df) - (df['Col_1'] > df['Col_2']).sum()