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

loop or iterate to determine if all columns from multiple datasets have the same name and position in Python

I have 15 datasets or data frames, let them be named data_1 to data_15. All suppose to have the same columns names. I would like to check if all columns have the same name and position before concatenate them. I concatenated them and I ended with an extra column because one column name of one dataset was misspelled. I used the following following code per dataset, but I would like to improve my skills and save time.

print(list(data_1))

The code I use to concatenate all datasets is the following:

pd.concat([data_1; data_2...data_15])

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 :

Put all the dataframes in a list, then use all() to test if the column names are all the same.

columns = list(data_1.columns.values)
df_list = [data_2, ..., data_15]

if all(list(df.columns.values) == columns for df in df_list):
    # code that concatenates all the dataframes
else:
    print("Columns don't match")
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