I'm trying to do a conditional vlookup but with pandas. Here's the data i'm using
n_age_scores
type n aging_n mini_n percent_n
new <30 days 0 0.5543
new 31-50 days 31 0.6446
new 51-100 days 51 0.3134
e_age_scores
type e aging_e mini_e percent_e
expansion <30 days 0 0.33543
expansion 31-50 days 31 0.4446
expansion 51-100 days 51 0.6134
Dataframe
type age score
new 33
new 12
expansion 3
new 4
expansion 100
What I want to do is populate score with the percent column of either dataframe based on if the row type
is new or expansion, an approximate match of value percent.
How do I do this with Pandas?
n_age_scores = aging_score_mapping.iloc[:,0:4]
e_age_scores = aging_score_mapping.iloc[:,-4:9]
if df['deal_type'] == 'Expansion':
df = merge.e_age_scores(df, on='age_score')
if df['deal_type'] == 'new':
df = merge.n_age_scores(df, on='age_score')
I'm not sure how to do this but I think i need to loop and merge with a approximate match and populate age_score with percent_n depending on the type.
Is this possible with pandas?
You can define your custom function to extract the data from the two dataframes and use it with apply .
If I understood correctly what you want, the code below shoud do the job.
I called age_map
the resulting dataframe (those with the score
column to be populated and collector
the function which extract the data from n_age_scores
and e_age_scores
.
def collector(row):
if row['type'] == 'new':
return n_age_scores.loc[n_age_scores['mini_n'] < row['age']].iloc[-1]['percent_n']
elif row['type'] == 'expansion':
return e_age_scores.loc[e_age_scores['mini_e'] < row['age']].iloc[-1]['percent_e']
age_map['score'] = age_map.apply(collector, axis=1)
Using the sample dataframes you provide, age_map
is:
type age score
0 new 33 0.64460
1 new 12 0.55430
2 expansion 3 0.33543
3 new 4 0.55430
4 expansion 100 0.61340
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.