0

I have a df "survived" that has a common column "contract_id" with another df(df2)

df2 has all different kinds of contracts, but i need to select only those that are present in survived.contract_id

df2[ all_survived['contract_id'] == df2['contract_id']] # not working

ValueError: Can only compare identically-labeled Series objects

I want to choose only those rows that have same contract id from 'survived df'

ERJAN
  • 23,696
  • 23
  • 72
  • 146
  • 1
    Use `df2[ df2['contract_id'].isin(all_survived['contract_id'])]` – jezrael Jun 03 '20 at 10:38
  • @jezrael, thx, another question - should it not be that dataframes be swapped? the number of rows in df2 is > num of rows in all_survived, so i m looking for df2[ all_survived['contract_id'].isin(df2['contract_id'])], but gives me "un alignable series", basically sizees dont match.. – ERJAN Jun 03 '20 at 10:59
  • No, it cannot be swapped, because output of `df2['contract_id'].isin(all_survived['contract_id'])` has to be mask with same size like `df2`. If use `df2[ all_survived['contract_id'].isin(df2['contract_id'])]` errror is expected, because get mask for `all_survived`. So yhis mask is possible use like `all_survived[ all_survived['contract_id'].isin(df2['contract_id'])]`. Btw, this is all True with general data, it means `all_survived.index != df2.index` – jezrael Jun 03 '20 at 11:03
  • @jezrael, i know this is overkill, but what i had before your code was merge() like this: df_report = df2.merge(all_survived, on='contract_id', how = 'inner') and it gives me diff result..but it should give same result.. – ERJAN Jun 03 '20 at 11:15
  • @jezrael, with your code isin() i get less rows around 18k , with merge() i get more rows.. i suspect merge() simply merges 2 df with inner join , so it only takes those contracts that do match.. – ERJAN Jun 03 '20 at 11:17
  • exacly you are right. – jezrael Jun 03 '20 at 11:18
  • @jezrael, yes, but why then your code isint() gives less rows? even though LOGICALLY, it should be the same - your code matches semantically same way as inner join.. ? this is mystery for me.. – ERJAN Jun 03 '20 at 11:26
  • hmmm, it is weird, not idea. – jezrael Jun 03 '20 at 11:27

0 Answers0