Get dplyr count of distinct in a readable way

How about this option: Use filter to filter out any rows where aa has NAs, then group the data by column bb and then summarise by counting the number of unique elements of column aa by group of bb. As you can see I’m making use of the pipe operator %>% which you can use to “pipe” or “chain” commands together when using dplyr. This helps … Read more

when to use if vs elif in python

I’ll expand out my comment to an answer. In the case that all cases return, these are indeed equivalent. What becomes important in choosing between them is then what is more readable. Your latter example uses the elif structure to explicitly state that the cases are mutually exclusive, rather than relying on the fact they are implicitly … Read more

What does if __name__ == “__main__”: do?

Short Answer It’s boilerplate code that protects users from accidentally invoking the script when they didn’t intend to. Here are some common problems when the guard is omitted from a script: If you import the guardless script in another script (e.g. import my_script_without_a_name_eq_main_guard), then the second script will trigger the first to run at import … Read more

What does if __name__ == “__main__”: do?

Short Answer It’s boilerplate code that protects users from accidentally invoking the script when they didn’t intend to. Here are some common problems when the guard is omitted from a script: If you import the guardless script in another script (e.g. import my_script_without_a_name_eq_main_guard), then the second script will trigger the first to run at import … Read more