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

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 :: knowing the sum null values in a specific row in pandas dataframe 
Python :: python turtle get mouse position 
Python :: dataframe to dictionary 
Python :: python dict key delete 
Python :: python vs c++ 
Python :: how to get bot voice channel discord.py 
Python :: how to rename rengeindex pandas 
Python :: tkinter get child in frame 
Python :: python kill all threads 
Python :: timedelta 
Python :: how to use turtle in python in python 3.9 
Python :: custom validation in django models 
Python :: python numpy array change axis 
Python :: tasks discord py 
Python :: django never_cache example 
Python :: how to move tkinter images 
Python :: how to file in python 
Python :: add a value to an existing field in pandas dataframe after checking conditions 
Python :: text to audio in python 
Python :: snakeCase 
Python :: python logger to different file 
Python :: date strftime python 
Python :: how to remove items from list in python 
Python :: docker django development and production 
Python :: python float print 2 digits 
Python :: how to cout in python 
Python :: numpy vector multiplication 
Python :: add fonts to matplotlib from a particular location 
Python :: how to display printed values without scientific notation python 
Python :: python url shortener 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =