首页 > 解决方案 > New target column based on a pattern

问题描述

Dataframe:

source    target              
jan       feb                               
mar       apr                 
jun                       
feb       aug                                            
apr       jul                                            
oct       dec                     
aug       nov       
dec       may                               

The output dataframe would be:

source    target    new_target              
jan       feb       nov                        
mar       apr       jul                  
jun                              
feb       aug       nov                                     
apr       jul       jul                                           
oct       dec       may              
aug       nov       nov
dec       may       may

The aim is to create new_targetcolumn based on a logic like - for example, jan in source has value feb in target. This in turn, feb in source has a value aug in target, and so on aug has nov in target column.

So the new_target column values for jan, feb and aug will be nov.

Thanks!

标签: pythonpandas

解决方案


首先创建一个用于映射目标的字典,然后将其应用于每个目标

m = dict((zip(df.source, df.target)))
def mapper(x):
    for i in range(len(m)):
        x = m.get(x, x)
    return x   

df["new_target"] = df.target.apply(mapper)

推荐阅读