簡體   English   中英

Pandas:命名dataframe的未完成列

[英]Pandas: Name the unmaed column of dataframe

我有 dataframe 有一個列,但它沒有任何名稱,如下圖所示。

在此處輸入圖像描述

現在我想命名未命名的庫倫。 我查看了現有線程( 重命名未命名列 pandas dataframe )並編寫了以下代碼:

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

但它沒有產生預期的結果。 誰能指出我在哪里犯了錯誤?

首先需要明確Pandas系列是一維數據,Series name就是你要的列名,打印的時候顯示在下面。 如果要獲得期望的列名出現在列值上方的效果,必須將數據類型轉換為 pandas 的 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)

演示展示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM