I have a datafame: What is the shortes / simples way to add a repeating counter column like this?: My feeling tells me, that there must be a one-line solution (maybe a bit longer). But all I can think of would be much longer and complex. How would you approach this? Answer try: OR If you have a custom index
Tag: counting
How can I group by two columns interchangeably?
How can I group by two columns interchangeably? For example, if I have this table and I want to get However, I get this instead when I use The entries (rows) that have the same names but exchanged are considered to be new entries, but i want to treat them the same way, can you please tell me a way
count the number of occurrences of a certain value in a dictionary in python?
If I have got something like this: If I want for example to count the number of occurrences for the “0” as a value without having to iterate the whole list, is that even possible and how? Answer As mentioned in THIS ANSWER using operator.countOf() is the way to go but you can also use a generator within sum() function
Tracking how many elements processed in generator
I have a problem in which I process documents from files using python generators. The number of files I need to process are not known in advance. Each file contain records which consumes considerable amount of memory. Due to that, generators are used to process records. Here is the summary of the code I am working on: My process_records function
Fast way of counting non-zero bits in positive integer
I need a fast way to count the number of bits in an integer in python. My current solution is but I am wondering if there is any faster way of doing this? PS: (i am representing a big 2D binary array as a single list of numbers and doing bitwise operations, and that brings the time down from hours