[英]Unable to change a column name in a dataframe
I'm working on some survey data and am looking to graph some of the results with standard deviation.我正在处理一些调查数据,并希望用标准偏差绘制一些结果。
I can successfully create the new column with the std values, but it names the column (Question 3, std).我可以使用 std 值成功创建新列,但它为列命名(问题 3,std)。 I've then tried to rename the column 'std', but the column name doesn't change.
然后我尝试重命名列“std”,但列名没有改变。
new_df = df.groupby('Respondants')[['Question 3']].agg({'std'})
std_df = df.merge(new_df, left_on=['Respondants'], right_index=True)
col_dict = {'Question 3': 'std'}
std_df.columns = [col_dict.get(x, x) for x in std_df.columns]
What am I missing?我错过了什么?
EDIT: The strange thing is that I've now tried:编辑:奇怪的是我现在尝试过:
new_df = df.groupby('Respondants')[['Question 3']].agg({'std'})
std_df = df.merge(new_df, left_on=['Respondants'], right_index=True)
std_df.rename(columns={'(Question 3, std)': 'std'}, inplace=True)
std_df.head(200)
and the result is the same.结果是一样的。 But if I try:
但如果我尝试:
new_df = df.groupby('Respondants')[['Question 3']].agg({'std'})
std_df = df.merge(new_df, left_on=['Respondants'], right_index=True)
std_df.rename(columns={'Question 1': 'whatever'}, inplace=True)
std_df.head(200)
That column name changes, so the code works, but not for the column that I needed.该列名称更改,因此代码有效,但不适用于我需要的列。
Use rename
function of the DataFrame:使用
rename
DataFrame 的 function :
std_df.rename(columns={'(Question 3, std)': 'std'}, inplace=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.