Pandas: Select Dataframe Columns Based On Another Dataframe's Columns
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.fram
Solution 1:
In[77]: df1[df1.columns.intersection(df2.columns)]Out[77]:
AB0161272383494510In[78]: df1[df1.columns.difference(df2.columns)]Out[78]:
C011112213314415
or similar, but not obvious:
In[92]: df1[list(set(df1) & set(df2))]Out[92]:
BA0611722833944105In[93]: df1[list(set(df1) - set(df2))]Out[93]:
C011112213314415
Solution 2:
Use isin
, dropna
:
df1[df1.isin(df2)].dropna(1)
AB0161272383494510
df1[~df1.isin(df2)].dropna(1)
C
011112213314415
Post a Comment for "Pandas: Select Dataframe Columns Based On Another Dataframe's Columns"