Bring Your Logic using Python
341 subscribers
109 photos
1 video
4 files
544 links
Logic building using Python : Materials, links, e-books and Code snippets
Download Telegram
By default, the new column is added at the end of #pandas DataFrame.

To insert a column at a specific column index, use

DataFrame.insert(loc, column, value)
#Tip #Python

bin() and int() convert between a binary string (prefixed with "0b") and an integer number:

>>> bin(101)
'0b1100101'
>>> int('0b00011001', 2)
25


That could be useful for bitwise binary operators like AND, OR or XOR:

>>> a, b = 101, 25
>>> a & b
1
>>> a | b
125
>>> a ^ b
124
Did you know that you can give #Python's print an iterator and a separator?

So in this example you don't need the for loop
Elegant #Python: use * to unpack a sequence.
#Python functions are first-class objects so you can access their attributes like this.
The #Python requests library lets you conveniently choose if you want to raise an exception or not.
Did you know #Python's random module has a function called "randrange" that lets you choose a random item from range(start, stop[, step]) ?
In #Python you can chain comparison operators ...

Instead of writing "1 < a and a < 10" you can write "1 < a < 10", a bit more concise.
🔴🟡🟢 Here is a three-way comparison of the three built-in string formatting methods in Python 🐍

If you want to include the same variable(s) twice or more, then I think that the `.format` method is the most convenient method.

Take a look for yourself
did you know that you can use functions as values in a Python dictionary? 🐍

You can define the lambda function as a reference to the keys of the dictionary, and then you can pass arguments to it and evaluate it.

😘 Unsolicited {dict} pic:
This can be reduced further.

Party trick to avoid the lambdas.