Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dropping mutliple columns with specific text of a dataframe within a list of dataframes python

I have a list of dataframes and each dataframe has columns that contains columns names such as "Unnamed 1", "Unnamed 2" etc

I want to drop all columns that contain the name "Unnamed" from each dataframe in the list of dataframes.

df_all = [df1,df2,df3]
df_all2 = []

for df in df_all:
    df = df[df.columns.drop(list(df.filter(regex='Unnamed')))]
    df_all2 = df.append
    df_all = df_all2 

This works, however, is there a more succinct method?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is a better way by using regex column filter with negative lookahead

df_all = [df.filter(regex=r'^(?!Unnamed)') for df in df_all]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading