Q:

find duplicated csv columns from list [python pandas]

I want to find duplicate columns from a list, so not just any columns.
example of correct csv looks like this:
col1, col2, col3, col4, custom, custom
1,2,3,4,test,test
4,3,2,1,test,test

list looks like this:
columnNames = ['col1', 'col2', 'col3', 'col4']

So when I run something like df.columns.duplicated() I don't want to it detect the duplicate 'custom' fields, only if there is more than one 'col1' column, or more than one 'col2' column, etc, and return True when one of those columns is found to be duplicated.
I found when including a duplicate 'colN' column name, col4 in the example, and I print it out, it shows me that index(['col1', 'col2', 'col3', 'col4', 'col4.1'], dtype='object')
No idea how to write that line of code.

A:

Use Index.isin + Index.duplicated to create a boolean mask:
c = df.columns.str.rsplit('.', n=1).str[0]
mask = c.isin(columnNames) & c.duplicated()

If want to find duplicated column names use boolean indexing with this mask:
dupe_cols = df.columns[mask]

