xxxxxxxxxx
df2.columns = stocks['Ticker'][:3]
[:3] is just use first 3
[5::] skip first 5
price price price
2021-01-11 131.90 15.00 179.07
2021-01-12 128.09 15.74 182.65
to
Ticker A AAL AAP
2021-01-11 131.90 15.00 179.07
2021-01-12 128.09 15.74 182.65
xxxxxxxxxx
df.rename(columns={'oldName1': 'newName1',
'oldName2': 'newName2'},
inplace=True, errors='raise')
# Make sure you set inplace to True if you want the change
# to be applied to the dataframe
xxxxxxxxxx
df.rename(columns={"old_col1": "new_col1", "old_col2": "new_col2"}, inplace=True)
xxxxxxxxxx
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
>>> df.rename(columns={"A": "a", "B": "c"})
a c
0 1 4
1 2 5
2 3 6
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
# 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
import pandas as pd
# Creating a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Renaming the 'A' column to 'X'
df.rename(columns={'A': 'X'}, inplace=True)
# Printing the updated dataframe
print(df)
xxxxxxxxxx
df.rename(columns = {'old_col1':'new_col1', 'old_col2':'new_col2'}, inplace = True)
xxxxxxxxxx
# Give new names to all columns
df.columns = ['A','B','C']
# Change specific column names
df.rename(columns={'old_col': 'A', 'abc': 'B'}, inplace=True)