我有一个数据框(此处输出:https://pastebin.com/7RCPsHet;可以使用 pd.DataFrame.from_dict(orient='tight') 读取),其中包含我想要总计的两列。它们看起来像:
Tiered Rates Prices
lower higher
year month
2022 11 41.36 0.00
12 74.42 0.00
2023 1 72.31 0.00
2 71.01 0.00
但是,当我尝试使用 + 符号添加它们时,出现以下错误:
monthly_pivot[('Tiered Rates Prices', 'higher')] + monthly_pivot[('Tiered Rates Prices', 'lower')]
Expected a one-dimensional object, got a DataFrame with 2 columns instead.
打印出表达式给了我这张全 NaN 的表。这有点奇怪,因为我原以为结果是一列。在这一点上,我认为最有可能的罪魁祸首是这个数据框具有“年/月”多重索引,并且我没有准确地调用这些列来用 + 运算符对它们进行求和。所以我想知道我哪里做错了?
Tiered Rates Prices
higher lower
year month
2022 11 NaN NaN
12 NaN NaN
2023 1 NaN NaN
2 NaN NaN
我能够将两列加在一起,结果列如下所示:
monthly_pivot[(('Tiered Rates Prices'))].sum(axis=1)
year month
2022 11 82.72
12 148.84
2023 1 144.62
2 142.02
假设这个可重现的输入:
monthly_pivot = pd.DataFrame.from_dict(
{
'index': [
(2022, 11),
(2022, 12),
(2023, 1),
(2023, 2),
(2023, 3),
(2023, 4),
(2023, 5),
],
'columns': [
('Tiered Rates Prices', 'lower'),
('Tiered Rates Prices', 'higher'),
],
'data': [
[41.36, 0.0],
[74.42, 0.0],
[72.31, 0.0],
[71.01, 0.0],
[65.05, 0.0],
[37.8, 0.0],
[38.36, 0.0],
],
'index_names': ['year', 'month'],
'column_names': [None, None],
},
orient='tight',
)
您的代码应该按预期工作:
monthly_pivot[('Tiered Rates Prices', 'higher')] + monthly_pivot[('Tiered Rates Prices', 'lower')]
year month
2022 11 41.36
12 74.42
2023 1 72.31
2 71.01
3 65.05
4 37.80
5 38.36
dtype: float64
要产生显示的输出,您需要将列选择为 2D(数据帧),而不是 1D(系列):
monthly_pivot[[('Tiered Rates Prices', 'higher')]] + monthly_pivot[[('Tiered Rates Prices', 'lower')]]
Tiered Rates Prices
higher lower
year month
2022 11 NaN NaN
12 NaN NaN
2023 1 NaN NaN
2 NaN NaN
3 NaN NaN
4 NaN NaN
5 NaN NaN
在这种情况下,列不会对齐,并且加法确实会产生 NaN。
确保
monthly_pivot[('Tiered Rates Prices', 'higher')]
(和其他列)的输出是系列:
type(monthly_pivot[('Tiered Rates Prices', 'higher')])
pandas.core.series.Series