I want to change the column labels of a Pandas DataFrame from ['$a', '$b', '$c', '$d', '$e'] to ['a', 'b', 'c', 'd', 'e']

 

You can change the column labels of a Pandas DataFrame using the .columns attribute and the .rename() method. Here's how you can change the column labels from ['$a', '$b', '$c', '$d', '$e'] to ['a', 'b', 'c', 'd', 'e']:

python
import pandas as pd # Create a DataFrame with initial column labels data = {'$a': [1, 2], '$b': [3, 4], '$c': [5, 6], '$d': [7, 8], '$e': [9, 10]} df = pd.DataFrame(data) # Display the original DataFrame print("Original DataFrame:") print(df) # Change the column labels new_columns = ['a', 'b', 'c', 'd', 'e'] df.columns = new_columns # Display the DataFrame with new column labels print("\nDataFrame with New Column Labels:") print(df)

In this example, the .columns attribute is used to directly modify the column labels of the DataFrame. After changing the column labels to ['a', 'b', 'c', 'd', 'e'], you will see the updated DataFrame.

Output:

bash
Original DataFrame: $a $b $c $d $e 0 1 3 5 7 9 1 2 4 6 8 10 DataFrame with New Column Labels: a b c d e 0 1 3 5 7 9 1 2 4 6 8 10

Now, the column labels have been changed to 'a', 'b', 'c', 'd', and 'e'.

Comments