KeyError: None of [Index([(….)])] are in the columns for a list of columns generated with df.columns
Question:
I have a df_a
that contains all columns. Then I have df_b
that contains a subset of this dataframe. I want to select the columns that are in df_b
from df_a
.
Why does the following code not work?
df_a[[df_b.columns]]
It throws a KeyError "None of [Index([(....), (....))], dtype='object)] are in the [columns]
. Why?
Answers:
Inner []
is redundant, you can try
df_a[df_b.columns]
# or
df_a.reindex(columns=df_b.columns)
I have a df_a
that contains all columns. Then I have df_b
that contains a subset of this dataframe. I want to select the columns that are in df_b
from df_a
.
Why does the following code not work?
df_a[[df_b.columns]]
It throws a KeyError "None of [Index([(....), (....))], dtype='object)] are in the [columns]
. Why?
Inner []
is redundant, you can try
df_a[df_b.columns]
# or
df_a.reindex(columns=df_b.columns)