The question is how to fill NaNs with most frequent levels for category column in pandas dataframe? In R randomForest package there is na.roughfix option : A completed data matrix or data frame. For numeric variables, NAs are replaced with column medians. For factor variables, NAs are replaced with the most f…
Tag: python
Search and filter pandas dataframe with regular expressions
I’d appreciate your help. I have a pandas dataframe. I want to search 3 columns of the dataframe using a regular expression, then return all rows that meet the search criteria, sorted by one of my columns. I would like to write this as a function so I can implement this logic with other criteria if poss…
How to get value counts for multiple columns at once in Pandas DataFrame?
Given a Pandas DataFrame that has multiple columns with categorical values (0 or 1), is it possible to conveniently get the value_counts for every column at the same time? For example, suppose I generate a DataFrame as follows: I can get a DataFrame like this: How do I conveniently get the value counts for ev…
Python for and if on one line
I have a issue with python. I make a simple list: I want create a “single line code” for find a string. for example, I have this code: But when I watch the variable is wrong (I find the last value of my list): Why does my variable contain the last element and not the element that I want to
Simple way to measure cell execution time in ipython notebook
I would like to get the time spent on the cell execution in addition to the original output from cell. To this end, I tried %%timeit -r1 -n1 but it doesn’t expose the variable defined within cell. %%time works for cell which only contains 1 statement. What’s the best way to do it? Update I have be…
What are type hints in Python 3.5?
One of the most talked-about features in Python 3.5 is type hints. An example of type hints is mentioned in this article and this one while also mentioning to use type hints responsibly. Can someone explain more about them and when they should be used and when not? Answer I would suggest reading PEP 483 and P…
Flask validates decorator multiple fields simultaneously
I have been using the @validates decorator in sqlalchemy.orm from flask to validate fields, and all has gone well as long as all of the fields are independent of one another such as: However, now I need to do some validation that will require access to field_one and field_two simultaneously. It looks like val…
How to use if and else statements to achieve Age Classifier program
I have an assignment in my Python class. It says: Write a program that asks the user to enter a person’s age. The program should display a message indicating whether the person is an infant, child, teenager, or adult. Here are the following guidelines: If the person is 1 year old or less, he or she is a…
Generator as function argument
Can anyone explain why passing a generator as the only positional argument to a function seems to have special rules? If we have: This works, as expected. This does not work, as expected. This works, as expected This works, but I don’t understand why. Shouldn’t it fail in the same way as 2) Answer…
django makemigrations not detecting new model
I used makemigrations earlier in order to make my Django app aware of the tables in my legacy MySql database, and it worked fine. It generated models.py. Now, I want to add a new “UserDetails” model to my app: After saving the file, I ran the following command in the command prompt: But makemigrat…