PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 28, 2022

[FIXED] How to Check CSV file headers for specific word in Python

 August 28, 2022     cell, csv, header, if-statement, python-3.x     No comments   

Issue

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?


Solution

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')


Answered By - Guillaume
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing