简体   繁体   English

在Python中重命名数据框中的列

[英]Rename columns in data frame in Python

I've completed an analysis and convert results into a data frame using result.to_frame(). 我已经完成了分析,并使用result.to_frame()将结果转换为数据框。 And the result looks like this: 结果看起来像这样:

在此处输入图片说明

The next step is to build a pivotal table based on column 1 and sum the counts. 下一步是基于第1列构建一个数据透视表,并对计数求和。 In order to do that, I want to label the first column as "query". 为了做到这一点,我想将第一列标记为“查询”。 I used this code, but the first column cannot be renamed. 我使用了此代码,但是第一列无法重命名。

df.rename(columns = {'query':'sum of counts'}, inplace = True)

I found it shows this data frame only has one column even though I saw two. 我发现它显示此数据帧只有一列,即使我看到了两列。

在此处输入图片说明

It looks like you indeed have 1 column, and the description is actually your index. 看起来您确实有1列,而说明实际上就是您的索引。 If your DataFrame is "df", try the following: 如果您的DataFrame是“ df”,请尝试以下操作:

df.shape # this should show (N, 1) where N is the number of records
df['Query'] = df.index # to pull a copy of the index into a column

You should be able to proceed normally then. 然后,您应该能够正常进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM