Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

python - Looping Through List And applying to rows in DataFrame where Filter is matched

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's not necessary to use apply, assign column 'perc' with the refer publish's day_zero_purchase_perc. Then use condition to filter which row to apply.

day_zero_purchase_perc = [
    {'Facebook': 0.950920245}, 
    {'Google': 0.147138229},  
    {'Other': 0.187124464}
]

benchmarks = dict()
for i in day_zero_purchase_perc:
    benchmarks = {**benchmarks, **i}
# benchmarks
# {'Facebook': 0.950920245, 'Google': 0.147138229, 'Other': 0.187124464}

df['perc'] = df['publisher'].map(benchmarks)

data = df
cond = True
cond &= (data['days_after_install'] == 0) 
cond &= (data['measure'] == 'Expected Purchases in 120 Days')
data.loc[cond, 'purchase_proj'] = data.loc[cond, 'value']/data.loc[cond, 'perc']

print(data)

    0 publisher    date  days_after_install                         measure  
    1  Facebook  Oct-20                   0  Expected Purchases in 120 Days   
    2  Facebook  Oct-20                   0                        Installs   
    3  Facebook  Oct-20                   1  Expected Purchases in 120 Days   
    4  Facebook  Oct-20                   1                        Installs   
    5     Other  Dec-20                   0  Expected Purchases in 120 Days   
    6     Other  Dec-20                   0                        Installs   

    0  value      perc  purchase_proj  
    1     57  0.950920      59.941936  
    2  24531  0.950920            NaN  
    3      9  0.950920            NaN  
    4      0  0.950920            NaN  
    5     31  0.187124     165.665137  
    6    152  0.187124            NaN  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...