Python Pandas / Sales force SOQL:如何使用 for 循环将列表传递给 SOQL 查询?
Python Pandas / Sales force SOQL: how to pass list to SOQL query using for loop?
我想做一个刺激列表的代码,并在 SOQL 查询中使用它来获取输出并附加到 excel sheet.
这是我的代码,有没有更简洁的方法来做到这一点。
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"
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)
我使用 dropna 解决了这个问题,以便能够将数据帧整洁地传递给 excel sheet。
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'))
我想做一个刺激列表的代码,并在 SOQL 查询中使用它来获取输出并附加到 excel sheet.
这是我的代码,有没有更简洁的方法来做到这一点。
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"
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)
我使用 dropna 解决了这个问题,以便能够将数据帧整洁地传递给 excel sheet。
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'))