python rename columns
时间: 2023-06-22 14:28:37 浏览: 183
python数据分析与可视化-15-获取和设置标签属性.ev4.rar-12-数据类型和约束.ev4.rar
To rename columns in Python, you can use the `rename` method of a pandas DataFrame. Here is an example code snippet:
```python
import pandas as pd
# create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# rename the columns
df = df.rename(columns={'A': 'new_A', 'B': 'new_B'})
# print the updated DataFrame
print(df)
```
In this example, we first create a sample DataFrame with columns 'A' and 'B'. We then use the `rename` method to rename the columns to 'new_A' and 'new_B'. Finally, we print the updated DataFrame using the `print` function.
阅读全文