I'm trying to subset a pandas
dataframe based on columns in another, similar dataframe. I can do this easily in R:
df1 <- data.frame(A=1:5, B=6:10, C=11:15)
df2 <- data.frame(A=1:5, B=6:10)
#Select columns in df1 that exist in df2
df1[df1 %in% df2]
A B
1 1 6
2 2 7
3 3 8
4 4 9
5 5 10
#Select columns in df1 that do not exist in df2
df1[!(df1 %in% df2)]
C
1 11
2 12
3 13
4 14
5 15
How can I do that with the pandas
dataframes below?
df1 = pd.DataFrame({'A': [1,2,3,4,5],'B': [6,7,8,9,10],'C': [11,12,13,14,15]})
df2 = pd.DataFrame({'A': [1,2,3,4,5],'B': [6,7,8,9,10],})