【问题标题】:Python Pandas / Sales force SOQL: how to pass list to SOQL query using for loop?Python Pandas / Sales force SOQL:如何使用 for 循环将列表传递给 SOQL 查询?
【发布时间】:2020-03-24 10:18:37
【问题描述】:

我想做一个对列表产生刺激的代码,并在 SOQL 查询中使用它来获取输出并附加到 excel 表中。

这是我的代码,有没有更简洁的方法可以做到这一点。

 ExlReport=pd.read_excel(ExlReportPath,sheet_name=ExlSheetName)
 CaseNumberList = [] IdList = [] IdList = ExlReport['Id']

 for Id in IdList:
             results = sf.query_all ("SELECT LastModifiedDate,Case,Id FROM Case_Note  WHERE Case = '%s' ORDER BY LastModifiedDate ASC" %
 soqlEscape(Id)) sf_df =
 pd.DataFrame(results['records']).drop(columns='attributes')

预期输出:

  LastModifiedDate             Case           Id 
0  2020-02-19T23:31:35.000+0000  xxxxxxxxxxxx  yyyyyyyyyyyy  
1  2020-02-19T23:31:43.000+0000  xxxxxxxxxxxx  yyyyyyyyyyyy 
2  2020-03-11T20:48:54.000+0000  xxxxxxxxxxxx  yyyyyyyyyyyy  

我得到的输出:

0  2020-02-19T23:31:35.000+0000  xxxxxxxxxxxx  yyyyyyyyyyyy  
1  2020-02-19T23:31:43.000+0000  xxxxxxxxxxxx  yyyyyyyyyyyy 
2  2020-03-11T20:48:54.000+0000  xxxxxxxxxxxx  yyyyyyyyyyyy  

 new_axis = axis.drop(labels, errors=errors)
  File "C:\xxxxxxx", line 5018, in drop
    raise KeyError(f"{labels[mask]} not found in axis")
KeyError: "['attributes'] not found in axis"

【问题讨论】:

    标签: python pandas salesforce soql


    【解决方案1】:
    for Id in IdList:
             results = sf.query_all ("SELECT LastModifiedDate,Case,"+Id+" FROM Case_Note  WHERE Case = '%s' ORDER BY LastModifiedDate ASC" % soqlEscape(Id))
    

    之后,我会将每个 ID 中的数据附加到 df:

    df.append(results)
    

    【讨论】:

    • 追加函数没有像我预期的那样工作,可能是因为我使用时某些 SOQL 查询返回 NaN 值:pd.DataFrame(results['records']).drop(columns='属性')我得到: new_axis = axis.drop(labels,errors=errors)文件“C:\ xxxxxxx”,第5018行,在drop raise KeyError(f“{labels [mask]} not found in axis”)KeyError: “['attributes'] 在轴中找不到”
    • 你能把 results.head(5).to_json() 的输出作为示例粘贴到这里吗
    • 我能够使用 dropna 对其进行排序。非常感谢你看这个。
    【解决方案2】:

    我通过使用 dropna 解决了这个问题,以便能够将数据框整齐地传递给 Excel 工作表。

    sf_df=(sf_df).dropna(axis=0,how='any')
    Appended_df=pd.DataFrame((sf_df).drop(columns='attributes'))
    

    检查这个link for dropna

    完整代码:

        import pandas as pd
        ExlReport=pd.read_excel(ExlReportPath,sheet_name=ExlSheetName)
        CaseNumberList = []
        IdList = [] 
        IdList = ExlReport['Id']
        Appended_df = pd.DataFrame()
        sf_df=pd.DataFrame()
    
    
        for Id in IdList:
               results = sf.query_all ("SELECT LastModifiedDate,Case,Id FROM Case_Note  WHERE Case='%s' ORDER BY LastModifiedDate ASC" % soqlEscape(Id)) 
               sf_df =pd.DataFrame(results['records']).drop(columns='attributes')
               df = pd.DataFrame(results['records'])
               sf_df = sf_df.append(df)
    
        sf_df=(sf_df).dropna(axis=0,how='any')
        Appended_df=pd.DataFrame((sf_df).drop(columns='attributes'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多