熊猫:命名数据框的未填充列

时间:2019-10-28 22:56:47

标签: python python-3.x pandas

我的数据框只有一列,但是没有任何名称,如下图所示。

enter image description here

现在,我想命名未命名的库伦。我查看了现有线程(Rename unnamed column pandas dataframe)并编写了以下代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

但是没有产生期望的结果。谁能指出我在哪里犯错了?

1 个答案:

答案 0 :(得分:0)

首先,需要明确的是Pandas系列是1D数据,系列名称是所需的列名称,并且在打印时显示在下方。如果要获得期望的列名出现在列值上方的效果,则必须将数据类型转换为熊猫的DataFrame类型。


演示

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)

Demo Display