I have a list containing customer ids:
clients = ['act_1078577379193828', 'act_1503882476478990', 'act_232830897389702']
I'm running an asynchronous job in Python that returns me campaign data from customers inside a For
:
for advertiser in clients:
async_job = AdAccount(advertiser).get_insights(fields=fields, params=params, is_async=True)
async_job.api_get()
while async_job[AdReportRun.Field.async_status]!= 'Job Completed':
time.sleep(1)
async_job.api_get()
time.sleep(1)
df = pd.DataFrame(async_job.get_result())
I am trying to create a DataFrame containing the data from all the customer ids in the list, but when I create it, it only brings in data from one customer.
I was able to get the results from the other customers using If
:
for advertiser in clients:
async_job = AdAccount(advertiser).get_insights(fields=fields, params=params, is_async=True)
async_job.api_get()
while async_job[AdReportRun.Field.async_status]!= 'Job Completed':
time.sleep(1)
async_job.api_get()
time.sleep(1)
if advertiser == 'act_1078577379193828':
df1 = pd.DataFrame(async_job.get_result())
elif advertiser == 'act_1503882476478990':
df2 = pd.DataFrame(async_job.get_result())
elif advertiser == 'act_232830897389702':
df3 = pd.DataFrame(async_job.get_result())
But since I'm going to use a list with more than 44 customer ids, I feel it gets very poorly optimized this way.
Do you know of another way to put all the data together in a single dataframe without so many if
?
*async_job.get_result()
it's where returns the data.
Output from async_job.get_result()
:
[<AdsInsights> {
"account_id": "232830897389702",
"account_name": "Advertiser_Account_Name",
"actions": [
{
"action_type": "link_click",
"value": "4"
},
{
"action_type": "post_engagement",
"value": "4"
},
{
"action_type": "page_engagement",
"value": "4"
}
],
"ad_name": "***",
"adset_name": "***",
"campaign_name": "***",
"clicks": "15",
"cpc": "0.568",
"cpm": "15.160142",
"ctr": "2.669039",
"date_start": "2022-08-26",
"date_stop": "2022-08-26",
"frequency": "1.05838",
"impressions": "562",
"inline_post_engagement": "4",
"objective": "LEAD_GENERATION",
"reach": "531",
"spend": "8.52"
},