rename columns
时间: 2023-08-04 20:08:28 浏览: 158
Sure, I can help you with that. To rename columns in a pandas DataFrame, you can use the `rename` method. Here's an example:
```python
import pandas as pd
# create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# print the original DataFrame
print(df)
# rename the columns
df = df.rename(columns={'A': 'new_A', 'B': 'new_B'})
# print the DataFrame with renamed columns
print(df)
```
This will output:
```
A B
0 1 4
1 2 5
2 3 6
new_A new_B
0 1 4
1 2 5
2 3 6
```
In this example, we created a DataFrame with columns 'A' and 'B'. We then used the `rename` method to rename these columns to 'new_A' and 'new_B', respectively.
相关问题
python rename columns
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.
df.rename(columns={})
这是一个 pandas 库中的 DataFrame 方法,用于重命名 DataFrame 的列名。其中,大括号中的内容将旧列名映射到新列名。例如,可以使用以下代码将列名“old_name”重命名为“new_name”:
df.rename(columns={"old_name": "new_name"})
阅读全文