【问题标题】:Querying Using Variables in Pandas在 Pandas 中使用变量进行查询
【发布时间】:2018-04-25 06:21:52
【问题描述】:

我正在尝试使用以下条件进行查询,但它抛出错误

last_plus30 = df1.Last + df1.Last*0.2
last_minus30 = df1.Last - df1.Last*0.2
data = pd.DataFrame(pd.read_json('abc.json')['result']['sell'])
print data.query('Rate < @last_plus30')

以下是错误:

ValueError: Can only compare identically-labeled Series objects

【问题讨论】:

  • print(last_plus30) 是什么?
  • 0 0.00001219 姓名:姓氏,dtype:float64

标签: python pandas querying


【解决方案1】:

您需要通过item 将一项系列转换为标量:

last_plus30 = (df1.Last + df1.Last*0.2).item()

错误表示您将 Series(一项)与列 Rate 进行比较。

【讨论】:

    猜你喜欢
    • 2022-05-14
    • 1970-01-01
    • 2015-08-01
    • 2017-05-10
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 2015-09-17
    • 2020-05-03
    相关资源
    最近更新 更多