【问题标题】:Length of Values Not Matching Length of Index值的长度与索引的长度不匹配
【发布时间】:2018-02-22 04:28:30
【问题描述】:

我正在使用 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"

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

【问题讨论】:

  • @DyZ - 将 df 更正为比较 - 它与比较数据框中的其他列具有相同的形状

标签: python pandas dataframe comparison cryptocurrency


【解决方案1】:

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)

【讨论】:

  • 工作 - 感谢您的解决方案和补充说明。
猜你喜欢
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 2018-12-19
  • 2020-05-29
  • 2020-06-02
  • 2022-01-13
相关资源
最近更新 更多