[Solved] pandas column selection using boolean values from another dataframe


Let’s take a simplified example, so I can show the process here.

language = pd.Index(['zh', 'zh', 'zh', 'zh', 'zh', 'zh', 'zh', 'zh', 'zh','zh','na', 'na', 'na', 'na', 'na', 'na', 'na', 'na', 'na', 'na'],
   dtype="object", name="Page")

web = pd.DataFrame(columns = range(len(language)))

web.shape
(0, 20)

language.shape
(0, 20)

So both have the same number of columns, and you want to filter web down to only those columns where the equivalent entry in language is 'zh'.

When I run your code:

English = web.iloc[:,language=='zh']

It doesn’t return an error, it returns what you are after. This leads me to believe there is some code we aren’t seeing here in your description.

Another solution to what you want would be:

English = web[[i for  i,j in zip(web.columns, language) if j == 'zh']]

which will also return only those columns in web where the equivalent in language is equal to a desired value.

solved pandas column selection using boolean values from another dataframe