What are the differences between strings 'True','False' and boolean True,False in Python
Question:
sorted_varieties = price_extremes.sort_values(by =['min','max'], ascending = ['False','False'])
variety
min
max
Pinot Grigio
4.0
70.0
Malbec-Syrah
4.0
78.0
White Blend
4.0
375.0
Tempranillo
4.0
600.0
sorted_varieties = price_extremes.sort_values(by =['min','max'], ascending = [False,False])
variety
min
max
Ramisco
495.0
495.0
Terrantez
236.0
236.0
Francisa
160.0
160.0
Rosenmuskateller
150.0
150.0
I got two different sorted values when I passed the ascending arguments in the list format of booleans in string and boolean formats. I expected to run into an error if I passed Boolean values as strings. But the code got executed without any error. How do the booleans in string 'True','False'
differ from boolean True, False
values in execution?
Answers:
A non-empty string evaluates as True
, so in your case it is similar to running:
sorted_varieties = price_extremes.sort_values(by=['min','max'],
ascending=[True, True])
sorted_varieties = price_extremes.sort_values(by =['min','max'], ascending = ['False','False'])
variety | min | max |
---|---|---|
Pinot Grigio | 4.0 | 70.0 |
Malbec-Syrah | 4.0 | 78.0 |
White Blend | 4.0 | 375.0 |
Tempranillo | 4.0 | 600.0 |
sorted_varieties = price_extremes.sort_values(by =['min','max'], ascending = [False,False])
variety | min | max |
---|---|---|
Ramisco | 495.0 | 495.0 |
Terrantez | 236.0 | 236.0 |
Francisa | 160.0 | 160.0 |
Rosenmuskateller | 150.0 | 150.0 |
I got two different sorted values when I passed the ascending arguments in the list format of booleans in string and boolean formats. I expected to run into an error if I passed Boolean values as strings. But the code got executed without any error. How do the booleans in string 'True','False'
differ from boolean True, False
values in execution?
A non-empty string evaluates as True
, so in your case it is similar to running:
sorted_varieties = price_extremes.sort_values(by=['min','max'],
ascending=[True, True])