Pandas replace non-zero values

2024/9/20 0:43:28

I know I can replace all nan values with df.fillna(0) and replace a single value with df.replace('-',1), but how can I replace all non-zero values with a single value?

Answer

Use boolean indexing:

df[df != 0] = value
https://en.xdnf.cn/q/72538.html

Related Q&A

Pandas percentage change using group by

Suppose I have the following DataFrame: df = pd.DataFrame({city: [a, a, a, b, b, c, d, d, d], year: [2013, 2014, 2016, 2015, 2016, 2013, 2016, 2017, 2018],value: [10, 12, 16, 20, 21, 11, 15, 13, 16]})A…

Django cannot find my static files

I am relatively new to web dev. and I am trying to build my first web application. I have my static folder in project_root/static but for some reason, I keep getting 404s when I run the server:Not Foun…

How can I find intersection of two large file efficiently using python?

I have two large files. Their contents looks like this:134430513125296589151963957125296589The file contains an unsorted list of ids. Some ids may appear more than one time in a single file. Now I want…

Failed to load the native TensorFlow runtime - TensorFlow 2.1

I have a desktop computer and a notebook, when I tried to install tensorflow on a notebook just by using pip install tensorflow it worked ok, then I tried the same on my desktop computer and when I tri…

(Python) Issues with directories that have special characters

OS: Windows server 03 Python ver: 2.7For the code below, its runs fine when I substitute "[email protected]" with "fuchida". If I use the email format for directory name I get the f…

LibCST: Converting arbitrary nodes to code

Is it possible to dump an arbitrary LibCST node into Python code? My use case is that I want to extract the code for functions that match a specific naming scheme. I can extract the FunctionDef nodes …

calculating the number of k-combinations with and without SciPy

Im puzzled by the fact that the function comb of SciPy appears to be slower than a naive Python implementation. This is the measured time for two equivalent programs solving the Problem 53 of Project E…

How to subclass requests in python through inheritance

I would like to specialize / subclass the requests package to add some method with custom functionality.I tried to do this:# concrete_requests.py import requestsclass concreteRequests(requests):def __i…

ipython debugger: full traceback on interactive pdb?

I recently switched from ipython0.10 to ipython0.11. In ipython0.11, I only see a small snippet of the full traceback when the python debugger engages (i.e. using %pdb), whereas in ipython0.10 Id see …

How to get all users in a list Twitter API?

Is there a way to access all members in a list? Currently, I can only see the first 20 members? Specifically, Im using python and tweepy.