import pandas as pd8 Rename DF
df = pd.DataFrame({'col one':[100, 200], 'col two':[300, 400]})
df| col one | col two | |
|---|---|---|
| 0 | 100 | 300 |
| 1 | 200 | 400 |
8.1 df.rename() Basic
df.rename({'col one':'col_one', 'col two':'col_two'}, axis='columns')| col_one | col_two | |
|---|---|---|
| 0 | 100 | 300 |
| 1 | 200 | 400 |
8.2 Rename from another DF
df2 = pd.DataFrame({'Col A':[100, 200], 'Col B':[300, 400]})
df2| Col A | Col B | |
|---|---|---|
| 0 | 100 | 300 |
| 1 | 200 | 400 |
df_variable = pd.DataFrame({"OldName": ["Col A", "Col B"],
"NewName": ["A", "B"]})
df_variable| OldName | NewName | |
|---|---|---|
| 0 | Col A | A |
| 1 | Col B | B |
dict(zip(df_variable["OldName"], df_variable["NewName"])){'Col A': 'A', 'Col B': 'B'}
df2.rename(dict(zip(df_variable["OldName"], df_variable["NewName"])), axis = 'columns')| A | B | |
|---|---|---|
| 0 | 100 | 300 |
| 1 | 200 | 400 |