[英]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.