xxxxxxxxxx
df['col_name'] = df['col_name'].str.replace('G', '1')
xxxxxxxxxx
df.rename(columns={"old_col1": "new_col1", "old_col2": "new_col2"}, inplace=True)
xxxxxxxxxx
df.rename({"current": "updated"}, axis=1, inplace=True)
print(df.dtypes)
xxxxxxxxxx
#suppy as dict the column name to replace
df1 = df.rename(columns={'Name': 'EmpName'})
print(df1)
xxxxxxxxxx
df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
# Or rename the existing DataFrame (rather than creating a copy)
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)
xxxxxxxxxx
In [11]: df['n'].replace({'a': 'x', 'b': 'y', 'c': 'w', 'd': 'z'})
Out[11]:
0 z
1 x
2 y
3 w
4 w
5 x
6 z
7 y
Name: n, dtype: object
In [12]: df['n'] = df['n'].replace({'a': 'x', 'b': 'y', 'c': 'w', 'd': 'z'})
xxxxxxxxxx
# Define df
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
# Option 1
df = df.rename({"A": "a", "B": "c"}, axis=1)
# or
df.rename({"A": "a", "B": "c"}, axis=1, inplace=True)
# Option 2
df = df.rename(columns={"A": "a", "B": "c"})
# or
df.rename(columns={"A": "a", "B": "c"}, inplace=True)
# Result
>>> df
a c
0 1 4
1 2 5
2 3 6
xxxxxxxxxx
df.rename(columns = {'old_col1':'new_col1', 'old_col2':'new_col2'}, inplace = True)
xxxxxxxxxx
dict = {
'Android': 'Android',
'Chrome OS': 'Chrome OS',
'Linux': 'Linux',
'Mac OS': 'macOS',
'No OS': 'No OS',
'Windows': 'Windows',
'macOS': 'macOS'
}
df['col'] = df['col'].map(dict)