Convert list to columns with Pandas

115 views Asked by At

I have a Dataframe connect to Facebook Insights API with the following fields:

fields = [
    'ad_name',
    'action_values',
    'reach',
    'spend',
    'impressions',
    'frequency',
    'campaign_name',
    'conversions',
    'cpc',
    'cpm',
    'ctr',
    'objective',
    'clicks',
    'cost_per_conversion',
    'inline_post_engagement',
    'actions',
]
params = {
    'date_presets' : 'maximum',
    'time_increment' : '1', # os dados podem ser mostrados durante todo o período utilizando o 'all_days' ou mensalmente ('monthly'). Nesse coloquei o valor '1' que é para quebrar a planilha por dia.
    'filtering': [],
    'level': 'ad',
    'export_columns': [],
    'export_format' : 'csv',
    'export_name' : 'beetools',
    'action_breakdowns' : 'action_type',
} 

My action field return a list in JSON with some data like page likes, app installs, conversions and event response.

But I want to convert this list to columns in my dataframe, I've tried to use Assign:

df = pd.DataFrame(AdAccount(ad_account_id).get_insights(fields, params))
df.assign(actions=actions)

But I don't know how to use it since actions wasn't defined.

That's what my actions look like:

<AdsInsights> {
    "actions": [
        {
            "action_type": "link_click",
            "value": "5"
        },
        {
            "action_type": "post_engagement",
            "value": "5"
        },
        {
            "action_type": "page_engagement",
            "value": "5"
        },
        {
            "action_type": "onsite_conversion.lead_grouped",
            "value": "1"
        },
        {
            "action_type": "leadgen_grouped",
            "value": "1"
        },
        {
            "action_type": "lead",
            "value": "1"
...
    "inline_post_engagement": "13",
    "objective": "LEAD_GENERATION",
    "reach": "1236",
    "spend": "45.86"
}]
0

There are 0 answers