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?
CodePudding user response:
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')
CodePudding user response:
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.