I am trying to figure out how I can loop through a list of dictionary values and apply the values of dictionaries to the rows in my dataframe that match specific filter conditions. At the moment I tried to solve for this by creating a function with the dataframe and list. Within that function I loop through the list and then have a nested loop for the dictionary which sets a new column in the dataframe to be equal to the value found in the rows in the dataframe that meet my filter criteria divided by the value of the object. While this setup accurately sets the column value for the first row that matches this criteria, it does not continue for other rows. Is this because of where I am setting the column value? Should I really use the .apply()
method?
Data Structure (analysis_df):
publisher date days_after_install measure value
0 Facebook Oct-20 0 Expected Purchases in 120 Days 57
1 Facebook Oct-20 0 Installs 24531
2 Facebook Oct-20 1 Expected Purchases in 120 Days 9
3 Facebook Oct-20 1 Installs 0
... ... ... ... ... ...
2881 Other Dec-20 0 Expected Purchases in 120 Days 31
2882 Other Dec-20 0 Installs 152
List of objects where pub
in the nested loop is the Key and perc
is the Value (day_zero_purchase_perc):
[
{'Facebook': 0.950920245},
{'Google': 0.147138229},
{'Other': 0.187124464}
]
Function:
def naive_projection(data, benchmarks):
# Loop through each day_zero_purchase_perc list dict
for obj in benchmarks:
print(obj)
# Loop through each publisher key-value pair in dict
for pub, perc in obj.items():
# Add projection column by day 0 installs / day 0 purchase percentage
data['purchase_proj'] = data[(data['days_after_install'] == 0) & (data['publisher'] == pub) & (data['measure'] == 'Expected Purchases in 120 Days')]['value'] / perc
print(data)
return data
naive_projection(analysis_df, day_zero_purchase_perc)
Result (Only the first row contains a value):
publisher date ... value purchase_proj
0 Facebook Oct-20 ... 57 59.941936
1 Facebook Oct-20 ... 24531 NaN
2 Facebook Oct-20 ... 9 NaN
3 Facebook Oct-20 ... 0 NaN
... ... ... ... ... ...
2881 Other Dec-20 ... 0 NaN
2882 Other Dec-20 ... 0 NaN
question from:
https://stackoverflow.com/questions/65890304/looping-through-list-and-applying-to-rows-in-dataframe-where-filter-is-matched 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…