Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas replace values in column based on condition

In [41]:
df.loc[df['First Season'] > 1990, 'First Season'] = 1
df

Out[41]:
                 Team  First Season  Total Games
0      Dallas Cowboys          1960          894
1       Chicago Bears          1920         1357
2   Green Bay Packers          1921         1339
3      Miami Dolphins          1966          792
4    Baltimore Ravens             1          326
5  San Franciso 49ers          1950         1003
Comment

pandas conditional replace values in a series

# np.where function works as follows:
import numpy as np

# E.g. 1 - Set column values based on if another column is greater than or equal to 50
df['X'] = np.where(df['Y'] >= 50, 'yes', 'no')

# E.g. 2 - Replace values over 20000 with 0, otherwise keep original value
df['my_value'] = np.where(df.my_value > 20000, 0, df.my_value)
Comment

pandas replace row values based on condition

In [41]:
df.loc[df['First Season'] > 1990, 'First Season'] = 1
df

Out[41]:
                 Team  First Season  Total Games
0      Dallas Cowboys          1960          894
1       Chicago Bears          1920         1357
2   Green Bay Packers          1921         1339
3      Miami Dolphins          1966          792
4    Baltimore Ravens             1          326
Comment

pandas replace values based on condition

df.loc[df['First Season'] > 1990, 'First Season'] = 1
Comment

replace values in a column by condition python

df.loc[df['employrate'] > 70, 'employrate'] = 7
Comment

replace column values/create new column based on another column values/condition in Pandas

df['New Column'] = np.where(df['A']==0, df['B'], df['A'])
Comment

panda replace row values based on condition in some existing column

df['column'].mask(df['column'] == 'original_value', new_value, inplace=True)
Comment

replace pandas column values based on condition

d.loc[d["conditionDisplayName"] == "Brand New", "conditionDisplayName"] = 6
d.loc[d["conditionDisplayName"] != "Brand New", "conditionDisplayName"] = 4
Comment

PREVIOUS NEXT
Code Example
Python :: string to list python 
Python :: python run bat in new cmd window 
Python :: var_dump in python 
Python :: code 
Python :: remove item from list 
Python :: get files in directory and subdirectory 
Python :: how to make a timer using python 
Python :: openmp for loop 
Python :: snake water gun game in python 
Python :: dict in dict in python 
Python :: python compare each item of one list 
Python :: how to code a trading bot in python 
Python :: print binary tree python 
Python :: python ceiling division 
Python :: install python macos catalina 
Python :: python condition question 
Python :: python is prime 
Python :: pandas if value present in df index 
Python :: python 
Python :: pyspark dataframe to dictionary 
Python :: snapchat api in python 
Python :: removing duplicates using json python 
Python :: newline in python gives an extra one 
Python :: switch case dictionary python 
Python :: axvline matplotlib 
Python :: k fold cross validation from scratch python 
Python :: pandas filter column greater than 
Python :: data frame 
Python :: how to declare a dictionary in python 
Python :: dictionary append value python 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =