gpt4 book ai didi

python - 值的长度与索引的长度不匹配

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:33 25 4
gpt4 key购买 nike

我正在利用 Python 的 Pandas 库进行一些加密货币分析。我生成了以下数据框:

        coin    old_price      current_price
0 BNBBTC 0.000949 0.000994
1 BNBETH 0.011472 0.012129
2 BNBUSDT 10.938950 9.358000
3 VENBNB 0.619480 0.635200

然后,我尝试比较 old_price 和 current_price 两列。

使用下面这行代码后:

comparison['sell'] = np.where((comparison['current_price'] >= comparison['old_price']))

我收到一条错误消息:

"ValueError: Length of values does not match length of index"

据我所知,数据框每列的数据数量相同。请指教,我们将不胜感激。

最佳答案

不带第二个和第三个可选参数的

np.where(condition) 返回 conditionTrue 的行索引数组。该数组通常比原始 DataFrame 短(在您的情况下,它只有一个值):

np.where(comparison['current_price'] >= comparison['old_price'])
#(array([2]),)

您需要的可能是这样的:

comparison['sell'] = (comparison['current_price'] >= comparison['old_price'])
#array([False, False, True, False], dtype=bool)

关于python - 值的长度与索引的长度不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48919684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com