Replace NaN values of pandas.DataFrame with values from list

2024/10/9 12:29:51

In a python script using the library pandas, I have a dataset of let's say 100 lines with a feature "X", containing 36 NaN values, and a list of size 36.

I want to replace all the 36 missing values of the column "X" by the 36 values I have in my list.

It's likely to be a dumb question, but I went through all the doc and couldn't find a way to do it.

Here's an example :

INPUT

Data:   X      Y1      82      3NaN    2NaN    71      2NaN    2

Filler

List: [8, 6, 3]

OUTPUT

Data:   X      Y1      82      38      26      71      23      2
Answer

Start with your dataframe df

print(df)X  Y
0  1.0  8
1  2.0  3
2  NaN  2
3  NaN  7
4  1.0  2
5  NaN  2

Define the values you want to fill with (Note: there must be the same number of elements in your filler list as NaN values in your dataframe)

filler = [8, 6, 3]

Filter your column (that contains the NaN values) and overwrite the selected rows with your filler

df.X[df.X.isnull()] = filler

df.loc[df.X.isnull(), 'X'] = filler

which gives:

print(df)X  Y
0  1.0  8
1  2.0  3
2  8.0  2
3  6.0  7
4  1.0  2
5  3.0  2
https://en.xdnf.cn/q/70021.html

Related Q&A

Boring Factorials in python

I am trying to understand and solve the following problem :Sameer and Arpit want to overcome their fear of Maths and so they have been recently practicing Maths problems a lot. Aman, their friendhas be…

The flask host adress in docker run

I want to run a flask application in Docker, with the flask simple http server. (Not gunicorn)I got a host setting problem. In the flask app.py, it should be work as the official tutorial, but it doesn…

Extracting text from pdf using Python and Pypdf2

I want to extract text from pdf file using Python and PYPDF package. This is my pdf fie and this is my code:import PyPDF2 opened_pdf = PyPDF2.PdfFileReader(test.pdf, rb)p=opened_pdf.getPage(0)p_text= p…

Is it possible to change turtles pen stroke?

I need to draw a bar graph using Pythons turtle graphics and I figured it would be easier to simply make the pen a thick square so I could draw the bars like that and not have to worry about making doz…

How to make a local Pypi mirror without internet access and with search available?

Im trying to make a complete local Pypi repository mirror with pip search feature on a server I can only connect an external hard drive to. To be clear, I dont want a simple caching system, the server …

Turn an application or script into a shell command

When I want to run my python applications from commandline (under ubuntu) I have to be in the directory where is the source code app.py and run the application with commandpython app.pyHow can I make i…

pytest - monkeypatch keyword argument default

Id like to test the default behavior of a function. I have the following:# app/foo.py DEFAULT_VALUE = hellodef bar(text=DEFAULT_VALUE):print(text)# test/test_app.py import appdef test_app(monkeypatch):…

How remove a program installed with distutils?

I have installed a python application with this setup.py:#!/usr/bin/env pythonfrom distutils.core import setup from libyouandme import APP_NAME, APP_DESCRIPTION, APP_VERSION, APP_AUTHORS, APP_HOMEPAGE,…

How to check which line of a Python script is being executed?

Ive got a Python script which is running on a Linux server for hours, crunching some numbers for me. Id like to check its progress, so Id like to see what line is being executed right now. If that was …

input to C++ executable python subprocess

I have a C++ executable which has the following lines of code in it /* Do some calculations */ . . for (int i=0; i<someNumber; i++){int inputData;std::cin >> inputData;std::cout<<"T…