Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

replace value in dataframe

# this will replace "Boston Celtics" with "Omega Warrior"
df.replace(to_replace ="Boston Celtics",
                 value ="Omega Warrior")
Comment

Change one value based on another value in pandas

df.loc[df.ID == 103, ['FirstName', 'LastName']] = 'Matt', 'Jones'
//or
import pandas
df = pandas.read_csv("test.csv")
df.loc[df.ID == 103, 'FirstName'] = "Matt"
df.loc[df.ID == 103, 'LastName'] = "Jones"
Comment

pandas replace values from another dataframe

In [36]:
df['Group'] = df['Group'].map(df1.set_index('Group')['Hotel'])
df

Out[36]:
         Date  Group  Family  Bonus
0  2011-06-09  Jamel  Laavin    456
1  2011-07-09  Frank  Grendy    679
2  2011-09-10   Luxy  Fantol    431
3  2011-11-02  Frank  Gondow    569
Comment

PREVIOUS NEXT
Code Example
Python :: check if a value is in index pandas dataframe 
Python :: python list comprehensions 
Python :: how to find the last occurrence of a character in a string in python 
Python :: precision accuracy recall python example 
Python :: python list equality 
Python :: flask form options 
Python :: how to append data in django queryset 
Python :: python how to restart thread 
Python :: django edit object foreign key id 
Python :: getting current user in django 
Python :: torch.utils.data.random_split(dataset, lengths) 
Python :: change the format of date in python 
Python :: turn a query set into a list django 
Python :: replace nan from another column 
Python :: set intersection 
Python :: how to standardize the image data to have values between 0 and 1 
Python :: test pypi 
Python :: print file in python 
Python :: string comparison in python 
Python :: How To Remove Elements From a Set using remove() function in python 
Python :: python describe 
Python :: python linkedin api 
Python :: python takes 2 positional arguments but 3 were given 
Python :: how to load a keras model with custom loss function 
Python :: Install Python2 and Python 3 
Python :: github downloader 
Python :: python 3.8 vs 3.10 
Python :: syntax of ternary operator 
Python :: print column name and index dataframe python 
Python :: flask echo server 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =