I have data from one data provider in very thin demographic units: Adults_18_21,Adults_22_24,Adults_25_27, etc.

Then in the original data frame that is built like this:

DemoDF = pd.DataFrame({'City':['NYC','Chicago','Nash'],'Adults_18_21':    [100,50,75],'Adults_22_24':[200,300,400],'Adults_25_27':[300,100,200]})

From another data provider, we get larger buckets of advertising data, Adults_18_27.

AdvertisingDF = pd.DataFrame({'City':     ['NYC','Chicago','Nash'],'Adults_18_27':[1,2,3]})

To multiply ad rates by demographic, I am currently creating a dictionary of tuples:

Compare_Buckets = {

Then creating new columns based on the tuples:

for key in Compare_Buckets.keys():
        DemoDF[key] = 0
        for value in Compare_Buckets[key]:
            DemoDF[key] += DemoDF[value]

I can then take the new resulting column and join it with the AdvertisingDF based on city and do any further functions I need. There are 40+ keys in the dictionary so I thought the for loop would work best. Is there a better way or a more memory efficient way?


You can do the whole filtering and sum using pandas' builtins:

for group, individuals in Compare_Buckets.items():
    DemoDF[group] = DemoDF.filter(items=individuals).sum(axis=0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.