Home > Mobile >  How do I put values calculated in column into another dataframe using name key?
How do I put values calculated in column into another dataframe using name key?

Time:12-15

Quick background check: I am working on a uni project due friday and am working on three datasets about mountain expeditions. One is named peaks and contains information about the mountain peaks. Another is named expeditions and gives info on the various trips that took place on those peaks. They both have in common the column peak_name (str). In expeditions, I calculated the success rate of each peak like this:

exped_peak=expeditions.groupby('peak_name').mean()

peak_success=exped_peak['success']
peak_success

Success is a column filled with booleans and shows if the expedition succeeded or failed. THe output is this:enter image description here

I want to create a column in peaks that gives the success rate for each peak. I did this: peaks['success_rate']=peaks_success , but my new column is filled with NaN values. How can I make it right?

I figure it probably has to do with the column 'peak_name' but I have never linked two dataframes like that so it's a bit confusing for me. Could anyone tell me how to make it work?

Thanks!

CodePudding user response:

You will want to merge the peak_success dataframe with the peaks dataframe based on the "peak_name" column and assign it back to the peaks dataframe.

peaks = peaks.merge(peak_success, how='left', on='peak_name')

This is similar to a left join in SQL where the merge() looks at the "peak_name" column in each dataframe and properly align the "success_rate" column into the peaks dataframe based on the matching values.

  • Related