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 replace data in specific columns with specific values

### replace one value ###
df["column"].replace("US","UK") # you can also use numerical values
### replace more than one value ###
df["column"].replace(["man","woman","child"],[1,2,3]) # you can also use numerical values
#   man ==> 1
# woman ==> 2
# child ==> 3
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

replace value in dataframe

# this will replace "Boston Celtics" with "Omega Warrior"
df.replace(to_replace ="Boston Celtics",
                 value ="Omega Warrior")
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 :: python last element in list 
Python :: How to create an infinite sequence of ids in python? 
Python :: python get average list in 2d array 
Python :: orderd dictionary pop vs del 
Python :: replace the jinja template value inside the dictionary python 
Python :: python prayer time 
Python :: views.home not found django 
Python :: get datafram colum names as list python 
Python :: selenium upload file python 
Python :: pandas groupby without reset index 
Python :: how to make a flask server in python 
Python :: rotate x labels in plots, matplotlib 
Python :: numpy distance between two points 
Python :: sort by column dataframe pyspark 
Python :: how to print a line letter by letter in python 
Python :: write a python program to find gcd of two numbers 
Python :: creating a new folder in python 
Python :: django q filter 
Python :: how to strip a list in python 
Python :: python print dictionary line by line 
Python :: python nameerror input 
Python :: django admin order by 
Python :: replacing values in pandas dataframe 
Python :: python cache return value 
Python :: How to ungrid something tkinter 
Python :: how to remove data from mongo db python 
Python :: plotly not showing in colab 
Python :: panda read data file 
Python :: how to add card in py-trello 
Python :: write specific columns to csv pandas 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =