In [1]:
import pandas as pd
In [2]:
df = pd.read_csv('data/src/sample_pandas_normal.csv')
df.iloc[1, 3] = 24
print(df)
In [3]:
print(df.replace('CA', 'California'))
In [4]:
print(df.replace(24, 100))
In [5]:
print(df.replace({'CA': 'California', 24: 100}))
In [6]:
print(df.replace(['CA', 24], ['California', 100]))
In [7]:
# print(df.replace(['CA', 24, 'NY'], ['California', 100]))
# ValueError: Replacement lists must match in length. Expecting 3 got 2
In [8]:
print(df.replace(['CA', 24], 'XXX'))
In [9]:
print(df.replace({'CA': 'NY', 'NY': 'XXX'}))
In [10]:
print(df.replace({'NY': 'XXX', 'CA': 'NY'}))
In [11]:
print(df.replace({'NY': 'XXX'}).replace({'CA': 'NY'}))
In [12]:
print(df.replace(['CA', 'NY'], ['NY', 'XXX']))
In [13]:
print(df.replace(['NY', 'CA'], ['XXX', 'NY']))
In [14]:
print(df.replace({'age': {24: 100}}))
In [15]:
print(df.replace({'age': {24: 100, 18: 0}, 'point': {24: 50}}))
In [16]:
# print(df.replace({'age': [[24, 18], [100, 0]], 'point': {24: 50}}))
# TypeError: If a nested mapping is passed, all values of the top level mapping must be mappings
In [17]:
print(df.replace({'age': 24, 'point': 70}, 100))
In [18]:
print(df.replace({'age': [24, 18], 'point': 70}, 100))
In [19]:
print(df.replace('li', 'LI'))
In [20]:
print(df.replace('(.*)li(.*)', r'\1LI\2', regex=True))
In [21]:
df['name'] = df['name'].str.replace('li', 'LI')
print(df)
In [22]:
df = pd.read_csv('data/src/sample_pandas_normal.csv')
print(df)
In [23]:
df.replace('CA', 'California', inplace=True)
print(df)