Skip to content

Tag: pandas

Count number of consecutive True in column, restart when False

I work with the following column in a pandas df: I want to add column B that counts the number of consecutive “True” in A. I want to restart everytime a “False” comes up. Desired output: Answer Using cumsum identify the blocks of rows where the values in column A stays True, then group…

Pandas apply() with axis=0 unexpected behaviour

I’m using the .apply() method in pandas. I get the same results when using axis=0 and axis=1. When using axis=0 I’d expect a series with four elements (indexed A, B, C, D) as a result. Can anyone tell me why the axis argument doesn’t work in this case? I’m adding a reproducible example…

Turn cell into False based another row/column Pandas

I have the following table of boolean values: index val1 val2 val3 val4 val5 val6 1 True False True True True False 2 False True True False True False 3 False False False True False True 4 True True True False False True I also have the following dictionary: How do I change the table so for every key column

best way to iterate through elements of pandas Series

All of the following seem to be working for iterating through the elements of a pandas Series. I’m sure there’s more ways of doing it. What are the differences and which is the best way? Answer TL;DR Iterating in pandas is an antipattern and can usually be avoided by vectorizing, applying, aggrega…