1

I have 2 dataframe like this picture. How to create column 'Team_used' for DF_1, returns results based on NEAREST 'date' of DF_1, DF_2 and 'list_id' of DF_2.

enter image description here

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
astonle
  • 91
  • 7
  • Could you include a reproducible example of the input data you're using? https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Nick ODell Nov 04 '20 at 02:59
  • Thank you for edit my question, Nick Odell. I can but I want to ask fast by picture, because coding takes longer and I just need the answer with a few coding suggestions. – astonle Nov 04 '20 at 04:35

1 Answers1

1

Try with explode then merge_asof

df1.date = pd.to_datetime(df1.date)
df2.date = pd.to_datetime(df2.date)
df1.sort_values('date',inplace=True)
df2.sort_values('date',inplace=True)
df2new = df2.explode('list_id_player').rename(columns = {'list_id_player' : 'id_player'})
df1 = df1.merge_asof(df2new , by = 'id_player', on = 'date', direction = 'nearest')
BENY
  • 317,841
  • 20
  • 164
  • 234