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 documentation 我仍然不知道该怎么做。它应该是具有单级索引的 <class 'pandas.core.frame.DataFrame'>
。每次使用 reset_index()
它都会创建一些新的(索引?)列,而使用 set_index('index')
会创建一个我不需要的两级列索引。
我可以想象有很多相应的情况,如果已经在某处回答,请提前抱歉,但到目前为止我还没有找到解决方案。任何帮助将不胜感激。
pd.DataFrame(cfd_appr).transpose().sum(axis=1).to_frame(1)
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
我有一个对象 <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 documentation 我仍然不知道该怎么做。它应该是具有单级索引的 <class 'pandas.core.frame.DataFrame'>
。每次使用 reset_index()
它都会创建一些新的(索引?)列,而使用 set_index('index')
会创建一个我不需要的两级列索引。
我可以想象有很多相应的情况,如果已经在某处回答,请提前抱歉,但到目前为止我还没有找到解决方案。任何帮助将不胜感激。
pd.DataFrame(cfd_appr).transpose().sum(axis=1).to_frame(1)
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