When you want to replace a column with a missing value (NaN) column by column

Overview

There is an article that says that the column containing NaN is extracted, but there was no one that specifically fetched each column or filled that column with another list, so I will summarize it this time. It was. As for the explanation, since it is a beginner, it may not be very helpful, but I hope you can see it to the extent that there is such a method.

Determining if NaN is included

The code that returns True when NaN is included somewhere in the line is If you want to return True only when all the lines are NaN, you can change any to all. In the data prepared this time, only the second line contains NaN.

df_judge = df.isnull().any(axis=1)

"""
0     False
1     False
2      True
3     False
4     False
5     False
6     False
7     False
8     False
9     False
"""

Insert the list in the column where NaN exists

First, I could check if True existed with an if statement, which was almost the same as calling an element of a list.

    for i in range(len(df)):
        if df_judge[i] == True:
            for j in range(len(df.columns)):
                #Round processing is processing that aligns to the first decimal place
                df.iloc[i,j] = average_list[j]

This time the goal was to insert the mean of the other columns into one column with NaN. Since the number of columns and the number of rows can be obtained with len (df) and len (df.columns), I used the double for statement. After it is determined that there is NaN in the if statement, the column is shifted one by one to the specified row and the average value is assigned. I used df.iloc [row, column] to specify the elements of the DataFrame. It's a pity that you can't insert each column directly, but you can handle it this way, so please refer to it.

Recommended Posts

When you want to replace a column with a missing value (NaN) column by column
When you want to sort a multidimensional list by multiple lines
Gist repository to use when you want to try a little with ansible
Settings when you want to run python-mecab with travis
When you want to filter with Django REST framework
When you want to play a game via Proxy
When you want to plt.save in a for statement
Solution when you want to use cv_bridge with python3 (virtualenv)
[Django] A memorandum when you want to communicate asynchronously [Python3]
[AWS] What to do when you want to pip with Lambda
Use aggdraw when you want to draw beautifully with pillow
When you want to register Django's initial data with relationships
When you want to hit a UNIX command on Python
When you want to print to standard output with print while testing with pytest
When you want to send an object with requests using flask
When you want to adjust the axis scale interval with APLpy
When you want to replace multiple characters in a string without using regular expressions in python3 series
When you want a long line break
When you want to use it as it is when using it with lambda memo
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
I made a program to notify you by LINE when switches arrive
Memorandum of means when you want to make machine learning with 50 images
I want to make a game with Python
Personal best practice template to use when you want to make MVP with Flask
If you want to create a Word Cloud.
When you want to update the chrome driver.
[OpenCV] When you want to check if it is read properly with imread
Python Note: When assigning a value to a string
How to remember when you forget a word
I want to write to a file with Python
If you want to make a discord bot with python, let's use a framework
When writing a test using DB with django, you may be able to do it faster by using `setUpTestData ()`
What to do when you want to receive files from a Windows client remotely
[Python] I want to use only index when looping a list with a for statement
[Linux] When you want to search for a specific character string from multiple files
When the variable you want to superscript with matplotlib is two or more characters
What to do if you don't want to use Japanese column names when using ortoolpy.logistics_network
I want to transition with a button in flask
I want to climb a mountain with reinforcement learning
Links to do what you want with Sublime Text
How to extract non-missing value nan data with pandas
I want to work with a robot in python.
I want to split a character string with hiragana
I want to manually create a legend with matplotlib
Things to do when you start developing with Django
I want to run a quantum computer with Python
How to extract non-missing value nan data with pandas
I want to bind a local variable with lambda
Make a note of what you want to do in the future with Raspberry Pi
I know? Data analysis using Python or things you want to use when you want with numpy
Useful operation when you want to solve all problems in multiple programming languages with Codewars
When you want to quickly translate a C # sample into another language such as VB
To know which bin a given value goes into when you have a bin delimiter in ndarray
A site to see when you want to read a machine learning paper but it seems difficult
If you want to use field names with hyphens when updating firestore data in python
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
I got a Value Error when using JUMAN ++ with PyKNP
Knowledge you need to know when programming competitive programming with Python2
I want to make a blog editor with django admin
I want to start a jupyter environment with one command
[python] A note when trying to use numpy with Cython