簡體   English   中英

DataFrame 中沒有名稱的列

[英]Column without name in DataFrame

我有一個通過counting DataFrame 中的值創建的 4 個series ,看起來像

hello world    2
aloha world        1
Name: hello, dtype: int64

我正在嘗試將這些數據連接到一個concat dataframe類的

all_Data = pd.concat([series1, series2, series3, series4], axis=1)
all_Data .columns = ["col1", "col2", "col3", "col4"]

它創造了這樣的東西

Name: hello, dtype: int64
           col1  col2   col3  col4
hello world      2    0.0  0.0      0.0
aloha world          1    1.0  0.0      1.0

它創建了 4 列 - 用於值,但第一列hello world等值沒有列名。 我正在將此dataframe轉換為dict之類的

all_Data .to_dict(orient='records')

但是沒有值的第一列(hello world,aloha world),我怎樣才能將它添加到df?

在連接 df 時添加 reset_index()。

all_Data = pd.concat([series1,series2,series3,series4],axis=1).reset_index()

然后根據需要更新列名: all_Data.columns=[column_name1,column_name2,...]

Pandas 系列是一維標記陣列。 在這里但是沒有第一列的值(你好世界,阿羅哈世界)你所指的第一列實際上是系列的標簽/索引。 現在,我怎樣才能將它添加到 df 中? . 您可以在連接系列時使用reset_index function 以將 this(index) 添加為列。 之后,當您將 DataFrame 轉換為字典時,它將具有值。

暫無
暫無

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

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