简体   繁体   English

Pandas:命名dataframe的未完成列

[英]Pandas: Name the unmaed column of dataframe

I have dataframe that has one column but it does not have any name as shown in the figure below.我有 dataframe 有一个列,但它没有任何名称,如下图所示。

在此处输入图像描述

Now I want to name the unnamed coulmn.现在我想命名未命名的库伦。 I looked at the existing thread ( Rename unnamed column pandas dataframe ) and wrote the following code:我查看了现有线程( 重命名未命名列 pandas dataframe )并编写了以下代码:

temp.rename(columns = {0:'SUBGROUP'},inplace=True)

but it didn't produce the desired result.但它没有产生预期的结果。 Could anyone point out where am I making the mistake?谁能指出我在哪里犯了错误?

First of all, it needs to be clear that the Pandas Series is 1D data, the Series name is the column name you want, and it is displayed below when printed.首先需要明确Pandas系列是一维数据,Series name就是你要的列名,打印的时候显示在下面。 If you want to get the effect that the column name you expect appears above the column value, you must convert the data type to the DataFrame type of pandas.如果要获得期望的列名出现在列值上方的效果,必须将数据类型转换为 pandas 的 DataFrame 类型。


Demo演示

import pandas as pd

# build Series temp with first 3 row data of your example
temp = pd.Series(data=["DA33", "", "FK33-2"], index=[70015,69999, 69998])
temp.index.name = "SITE_NUMBER"
temp.name = "FEEDERID"
print(type(temp))
print(temp)

# transform type from Series to DataFrame
# you see the name of Series should beoome the column name of DataFrame
temp = pd.DataFrame(data=temp)
print(type(temp))
print(temp)

演示展示

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

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