簡體   English   中英

Pandas 系列到 Pandas 數據框

[英]Pandas Series to Pandas Dataframe

我有一個對象<class 'pandas.core.series.Series'>看起來像這樣:

DataFrame(cfd_appr).transpose().sum(axis=1)
2022.12.06_Bild 2.txt     524.0
2022.12.06_Bild 3.txt     620.0
2022.12.06_Bild 4.txt     535.0
2022.12.06_Bild 5.txt     799.0
2022.12.06_Bild 6.txt    1032.0
2022.12.06_Bild 8.txt       4.0
dtype: float64

DataFrame(some_cfd).transpose().sum(axis=1) 現在我需要對其進行轉換,使其如下所示:

                              1
2022.12.06_Bild 2.txt     524.0
2022.12.06_Bild 3.txt     620.0
2022.12.06_Bild 4.txt     535.0
2022.12.06_Bild 5.txt     799.0
2022.12.06_Bild 6.txt    1032.0
2022.12.06_Bild 8.txt       4.0

在閱讀了Pandas 文檔數小時后,我仍然無法弄清楚如何做到這一點。 它應該是一個帶有單級索引的<class 'pandas.core.frame.DataFrame'> 每次使用reset_index()它都會創建一些新的(索引?)列,並使用set_index('index')創建一個我不需要的兩級列索引。

我可以想象那里有很多相應的案例,如果已經在某處得到了回答,請提前抱歉,但到目前為止我還沒有找到解決方案。 任何幫助,將不勝感激。

to_frame可用於將Series轉換為DataFrame

series = DataFrame(some_cfd).transpose().sum(axis=1)

# The provided name (1) will substitute for the series name
df = series.to_frame(1)

輸出應該是:

>>> print(df)

                              1
2022.12.06_Bild 2.txt     524.0
2022.12.06_Bild 3.txt     620.0
2022.12.06_Bild 4.txt     535.0
2022.12.06_Bild 5.txt     799.0
2022.12.06_Bild 6.txt    1032.0
2022.12.06_Bild 8.txt       4.0

使用Series.to_frame

pd.DataFrame(cfd_appr).transpose().sum(axis=1).to_frame(1)

暫無
暫無

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

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