【问题标题】:Dataframes sql problem in comparison of two sums比较两个总和的Dataframes sql问题
【发布时间】:2020-04-07 18:41:11
【问题描述】:

我想比较我的 sql 数据库中的 2 个总和。 我收到此错误。 ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

import pandas as pd
import cx_Oracle
from pprint import pprint

connection = cx_Oracle.connect("student_ps", "oracle", "xx.xx.xx.xx.x/oracle")

query="""SELECT nume_client, suma_solicitata, suma_depozit, fidelitate
 FROM t_clienti_leasing where varsta>30 and suma_solicitata>10000"""

df = pd.read_sql(query, con=connection, params=None)
pprint(df)
connection.close()

if(df['SUMA_DEPOZIT'] >= df['SUMA_SOLICITATA']):
    df["FIDELITATE"]=="5"

print(df)

【问题讨论】:

    标签: python sql pandas dataframe


    【解决方案1】:

    这是因为你不能在 if 语句中使用两个系列的比较。原因是比较两个系列会产生一个新的布尔系列,指示满足条件的索引。

    你应该像这样使用 .loc :

    df.loc[df['SUMA_DEPOZIT'] >= df['SUMA_SOLICITATA'],"FIDELITATE"] = 5
    

    希望对你有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 2022-01-10
      • 2021-11-18
      相关资源
      最近更新 更多