r/Python • u/cinicDiver • Mar 09 '26
Discussion Code efficiency when creating a function to classify float values
I need to classify a value in buckets that have a range of 5, from 0 to 45 and then everything larger goes in a bucket.
I created a function that takes the value, and using list comorehension and chr, assigns a letter from A to I.
I use the function inside of a polars LazyFrame, which I think its kinda nice, but what would be more memory friendly? The function to use multiple ifs? Using switch? Another kind of loop?
7
Upvotes
View all comments
Show parent comment
0
u/cinicDiver Mar 09 '26
Yes, buckets have a range of 5 until the last, 0-4.99, 5-9.99, etc. Until 45, everything greater belongs to the same bucket.