Skip to content
Advertisement

Extract data from pandas dataframe columns with duplicate names

I have a dataframe which has duplicate column names:

JavaScript

…and want to convert it into two dataframes; one only of “Accepted” columns and other for “Reject” Columns:

df1:

JavaScript

df2:

JavaScript

Tried:

JavaScript

… but this only gives the first column matching this name.

Advertisement

Answer

If select one column with same name are selected all columns with same name in DataFrame:

JavaScript

Then is possible deduplicate columns:

JavaScript

EDIT: If get only first column name it means there are not duplicated columns names, so is possible use DataFrame.filter:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement