简体   繁体   中英

Rename columns in data frame in Python

I've completed an analysis and convert results into a data frame using 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. 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. If your DataFrame is "df", try the following:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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