Matt M
Matt M

Reputation: 309

Apply Zip function on Pandas Dataframe using a For Loop Argument

I have a function called handle text that renames values in dataframe columns:

def handle_text(txt):
if txt.lower()[:6] == 'deu_ga': 
    return 'Western Europe', 'Germany'
elif txt.lower()[:6] == 'fra_ga':
    return 'Western Europe', 'France'
return 'Other', 'Other'

I apply handle_text on various dataframes in the following way:

campaigns_df['Region'], campaigns_df['Market']  = zip(*campaigns_df['Campaign Name'].apply(handle_text))

atlas_df['Region'], atlas_df['Market']  = zip(*atlas_df['Campaign Name'].apply(handle_text))

flashtalking_df['Region'], flashtalking_df['Market']  = zip(*flashtalking_df['Campaign Name'].apply(handle_text))

I was wondering if there was a way to do a for loop to apply the function to various dfs at once:

dataframes = [atlas_df, flashtalking_df, innovid_df, ias_viewability_df, ias_fraud_df]
columns_df = ['Campaign Name']

for df in dataframes:
    for column in df.columns:
        if column in columns_df:
            zip(df.column.apply(handle_text))

However the error I get is:

AttributeError: 'DataFrame' object has no attribute 'column'

Upvotes: 1

Views: 6465

Answers (2)

Matt M
Matt M

Reputation: 309

I managed to solve it like this:

dataframes = [atlas_df, flashtalking_df, innovid_df, ias_viewability_df, ias_fraud_df, mediaplan_df]
columns_df = 'Campaign Name'

for df in dataframes:
    df['Region'], df['Market'] = zip(*df[columns_df].apply(handle_text))

Upvotes: 2

jezrael
jezrael

Reputation: 863166

Need change attribute acces by . to more general by []:

zip(df.column.apply(handle_text))

to

zip(df[column].apply(handle_text))

EDIT:

Better solution:

atlas_df = pd.DataFrame({'Campaign Name':['deu_gathf', 'deu_gahf', 'fra_gagg'],'another_col':[1,2,3]})
flashtalking_df = pd.DataFrame({'Campaign Name':['deu_gahf','fra_ga', 'deu_gatt'],'another_col':[4,5,6]})

dataframes = [atlas_df, flashtalking_df]
columns_df = 'Campaign Name'

You can map by dict and then create new columns:

d = {'deu_ga': ['Western Europe','Germany'], 'fra_ga':['Western Europe','France']}

for df in dataframes:
    df[['Region','Market']] = pd.DataFrame(df[columns_df].str.lower()
                                                         .str[:6]
                                                         .map(d)
                                                         .values.tolist())

     #print (df)

print (atlas_df)
  Campaign Name  another_col          Region   Market
0     deu_gathf            1  Western Europe  Germany
1      deu_gahf            2  Western Europe  Germany
2      fra_gagg            3  Western Europe   France

print (flashtalking_df)
  Campaign Name  another_col          Region   Market
0      deu_gahf            4  Western Europe  Germany
1        fra_ga            5  Western Europe   France
2      deu_gatt            6  Western Europe  Germany

Upvotes: 0

Related Questions