0

I have two dataframes (498 rows and 249 rows) The index labels are objects I want to drop the columns which do not appear in the smaller DF so that both are 249 rows

INDEX DF1 (249 rows)
AB111  
AB222
AB333
AB444
AB555
AB666
AB777
AB888

INDEX DF2 (498 rows)
AB111 
dd222 
AB222
dd333
AB333
dd666
AB444
dd999
AB555
dd000
AB666
AB777
AB888

uncommon_indices = np.setdiff1d(df_patient_info_train.index.values, df_rnaseq_dropped.values)


uncommom_tolist = uncommon_indices.tolist()


wanted = df_rnaseq_dropped.drop(uncommon_tolist, axis=0, inplace=True)

ERROR: .....

not found in axis
cucurbit
  • 1,422
  • 1
  • 13
  • 32
May
  • 23
  • 4
  • Answered here: https://stackoverflow.com/questions/28901683/pandas-get-rows-which-are-not-in-other-dataframe – Yash Dec 15 '22 at 21:42
  • Does this answer your question? [pandas get rows which are NOT in other dataframe](https://stackoverflow.com/questions/28901683/pandas-get-rows-which-are-not-in-other-dataframe) – kmkurn Dec 16 '22 at 02:21

1 Answers1

0
import numpy as np
liste1=DF1.index.values.tolist()#list of DF1 index
liste2=DF1.index.values.tolist()#list of DF2 index
in_DF2_notin_DF1=np.setdiff1d(liste2,liste1) # index that exist in DF2 and not exestion in DF 
df.drop(in_DF2_notin_DF1, inplace=True) # drop index in list in_DF2_notin_DF1
phœnix
  • 367
  • 1
  • 9