Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas update with condition

import pandas as pd
import numpy as np

df = pd.DataFrame({'value':np.arange(1000000)})

# Solution 1 - Fastest :
df['value'] = np.where(df['value'] > 20000, 0, df['value'])

# Solution 2:
df.loc[df['value'] > 20000, 'value'] = 0

# Solution 3:
df['value'] = df['value'].mask(df['value'] > 20000, 0)

# Solution 4 - Slowest, note that df.where applies where condition is wrong:
df['a'] = df.where(df.a <= 20000, 0)
Comment

PREVIOUS NEXT
Code Example
Python :: python regex replace all non alphanumeric characters 
Python :: python calculate time taken 
Python :: python nested functions get variables from function scope 
Python :: how to read video in opencv python 
Python :: how to remove integer from string in python 
Python :: python sort a list of tuples 
Python :: pyttsx3 save to file 
Python :: pandas select all columns except one 
Python :: save df to txt 
Python :: django reset database 
Python :: sort python nested list according to a value 
Python :: clear multiprocessing queue python 
Python :: use selenium without opening browser 
Python :: pyspark import f 
Python :: pandas convert all column names to lowercase 
Python :: keras model load 
Python :: pandas add dataframe to the bottom of another 
Python :: get date and time in python 
Python :: how to check for a particular word in a text file using python 
Python :: how to send a message in a specific channel discord.py 
Python :: flask secret key generator 
Python :: image in cv2 
Python :: django versatileimagefield 
Python :: join list with comma python 
Python :: fill missing values with 0 pandas 
Python :: change directory in python os 
Python :: remove whitespace around figure matplotlib 
Python :: pylint no name in module cv2 
Python :: how to get pc name with python 
Python :: df.drop index 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =