简体   繁体   English

拆分包含嵌套列表和一个列表的 map 元素的字典项

[英]Split dictionary items containing nested lists and map elements of one list

I currently have a dictionary of lists that i am trying to map each list element to a customer id and by brand id.我目前有一个列表字典,我正在尝试将每个列表元素 map 到客户 ID 和品牌 ID。

So far, this is what I come up with - the issue is that i am missing the mapping between brand id and customer ids.到目前为止,这就是我想出的——问题是我缺少品牌 ID 和客户 ID 之间的映射。

Apologies for not sharing the full inputs for replication as it contains api keys/secrets.很抱歉没有共享完整的复制输入,因为它包含 api 密钥/秘密。

The goal is to have a dictionary per customer id belonging to the same brand id.目标是为每个客户 ID 拥有一个属于同一品牌 ID 的字典。 Currently, the loop is iterating over the length of brand ids so coercing customer id into a nested list.目前,循环正在迭代品牌 ID 的长度,因此将客户 ID 强制转换为嵌套列表。 Attached are also snippet of current versus desired output.随附的还有当前与所需 output 的片段。

*df_dict below is a dictionary of dataframes per brand id *df_dict 下面是每个品牌 ID 的数据框字典

df_dict = {}
for brand, values in reduced_df_send_message_now.groupby('brand_id'):
    df_dict.update({'brand_' + str(brand) : values.reset_index(drop=True)})
    
customers = [['1704283852'], ['1542053114', '1268330200', '1006435031']]
casino_ids = [['2000'], ['2080', '2080', '2080']]
batch_list = []
for c, i in enumerate(df_dict):
    batch = Batch()
    batch.user_identities = {'customer_id': customers[c]}
    batch.user_attributes = {
        'casino_id': casino_ids[c]
        }
    print(batch)
    batch_list.append(batch)

# current vs target split records

current = dict(
    {'api_key': None,
     'application_info': None,
     'consent_state': None,
     'context': None,
     'deleted_user_attributes': None,
     'device_info': None,
     'environment': None,
     'events': None,
     'ip': None,
     'mp_deviceid': None,
     'mpid': None,
     'schema_version': None,
     'source_request_id': None,
     'user_attributes': {'brand_id': ['2080', '2080', '2080']},
     'user_identities': {'customer_id': ['1542053114', '1268330200', '1006435031']}
     }
    )
    

target = dict(
    {'api_key': None,
     'application_info': None,
     'consent_state': None,
     'context': None,
     'deleted_user_attributes': None,
     'device_info': None,
     'environment': None,
     'events': None,
     'ip': None,
     'mp_deviceid': None,
     'mpid': None,
     'schema_version': None,
     'source_request_id': None,
     'user_attributes': {'brand_id': '2080'},
     'user_identities': {'customer_id': '1542053114'}
     }
    )

target2 = dict(
    {'api_key': None,
     'application_info': None,
     'consent_state': None,
     'context': None,
     'deleted_user_attributes': None,
     'device_info': None,
     'environment': None,
     'events': None,
     'ip': None,
     'mp_deviceid': None,
     'mpid': None,
     'schema_version': None,
     'source_request_id': None,
     'user_attributes': {'brand_id': '2080'},
     'user_identities': {'customer_id': '1268330200'}
     }
    )

target3 = dict(
    {'api_key': None,
     'application_info': None,
     'consent_state': None,
     'context': None,
     'deleted_user_attributes': None,
     'device_info': None,
     'environment': None,
     'events': None,
     'ip': None,
     'mp_deviceid': None,
     'mpid': None,
     'schema_version': None,
     'source_request_id': None,
     'user_attributes': {'brand_id': '2080'},
     'user_identities': {'customer_id': '1006435031'}
     }
    )

I think i figured it out - results match what i was trying to achieve (in the post).我想我想通了 - 结果与我试图实现的目标相匹配(在帖子中)。 below is the code i used to split customer dictionary by brand id - ** the following was also fixed by last line**: only issue is that the function has to be called at the end with the brand id as argument (i am only working with 3 brand ids for now) but feel free to suggest a dynamic way of looping through brand ids to catch new ones (that are currently not part of the pull).下面是我用来按品牌 ID 拆分客户字典的代码 - **最后一行也修复了以下内容**:唯一的问题是 function 必须在最后以品牌 ID 作为参数调用(我只是目前使用 3 个品牌 ID),但可以随意提出一种动态的方式来循环品牌 ID 以捕捉新的 ID(目前不属于拉动的一部分)。

df_dict = {}
for brand, values in reduced_df_send_message_now.groupby('brand_id'):
    df_dict.update({str(brand) : values.reset_index(drop=True)})
    
# function to transforme each column needed to a list so that we can include them as lists to dicts in batch further below
def col_to_list(df):
    
    if df.shape[0] > 0:
    # customers = df[['customerid', 'brand_id']].values.tolist()
        customers = df['customerid'].values.tolist()
        brand_ids = df['brand_id'].values.tolist()
        casino_ids = df['signup_casino_id'].values.tolist()
        balances = df['total_balance'].values.tolist()
        free_bet_cash_balance = df['free_bet_cash_balance'].values.tolist()
        bet_churn_cat = df['bet_churn_category'].values.tolist()
        bet_churn_score = df['customer_lapsing_factor'].values.tolist()
        login_churn_cat = df['login_churn_category'].values.tolist()
        login_churn_score = df['login_churn_score'].values.tolist()
        group_type = df['group_type'].values.tolist()
        api_key = df['key'].values.tolist()
        api_secret = df['secret'].values.tolist()

        return customers, brand_ids, casino_ids, balances, free_bet_cash_balance, bet_churn_cat,\
            bet_churn_score, login_churn_cat, login_churn_score, group_type, api_key, api_secret
    else:
        None

def return_needed_lists_by_brand(brand_id):
    
    list_names = ['customer_id', 'casino_id', 'balances'] 
                  
    if len(df_dict.keys()) == 0:
        return  
    if brand_id not in df_dict.keys():
        return  
    else:
        customers = col_to_list(df_dict[brand_id])[0]
        casino_ids = col_to_list(df_dict[brand_id])[2]
        balances = col_to_list(df_dict[brand_id])[3]

    return dict(zip(list_names, [customers, casino_ids]))


def upload_brand(brand_id):   
    batch_list = []
    if  brand_id not in df_dict.keys():
        return
    for i in range(len(return_needed_lists_by_brand(brand_id)['customer_id'])):
        batch = Batch()
        try:
            batch.user_identities = {'customer_id': return_needed_lists_by_brand(brand_id)['customer_id'][i]}
            batch.user_attributes = {
            'casino_id': return_needed_lists_by_brand(brand_id)['casino_id'][i]
             }
            print(batch)
        except:
                pass
        batch_list.append(batch)


#upload_brand("213")

# final upload
for i in brand_id_list:
    upload_brand(str(i))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM