how to rename a column
时间: 2023-12-16 12:03:28 浏览: 111
log4j:ERROR Failed to rename
To rename a column in a pandas DataFrame, you can use the `rename()` method. Here's an example:
```python
import pandas as pd
# Create an example DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Rename a specific column
df.rename(columns={'A': 'NewColumn'}, inplace=True)
print(df)
```
Output:
```
NewColumn B
0 1 4
1 2 5
2 3 6
```
In the above code, we use the `rename()` method to rename the column `'A'` to `'NewColumn'`. We pass a dictionary to the `columns` parameter, where the keys are the current column names and the values are the new column names. The `inplace=True` parameter ensures that the DataFrame is modified in-place. If you don't want to modify the original DataFrame, you can assign the result of `rename()` to a new DataFrame or the same variable.
阅读全文