0

I have several CSV files and their headers are similar for the first 10 columns and then they are different. So I need to check each of the CSV files and see if in the column 11 they have word ABC or XYZ or SOS so then I can apply my other function on each CSV depending on the header. Can anyone please help me with the if conditions that I need to apply to read the headers and compare column 11 in header with the strings I mentioned?

2 Answers 2

1

I'd use glob to loop for each csv, and pandas to read the columns names.

import glob

path = r"*.csv"  # The path to the folder containing your csv files

for file_name in glob.glob(path):  # Looping through the list of paths 
    df = pd.read_csv(file_name)

    list_of_column_names = list(df.columns)  # Getting the headers of your dataframe

    if 'ABC' in str(list_of_column_names[11]):
        print('ABC case')
    elif 'XYZ' in str(list_of_column_names[11]):
        print('XYZ case')
    elif 'SOS' in str(list_of_column_names[11]):
        print('SOS case')
    else:
        print('Other Case')
Sign up to request clarification or add additional context in comments.

Comments

0

In Java you have libraries to manage csv like Super CSV. And for Excel like Jakarta Apache POI. In Python I don't know if exists libraries like this.

1 Comment

Thanks for your answer. There is now a verified answer which worked just fine for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.