If -Else condition in Pandas to create CSV in separate folders

333 views Asked by At

I am new to python and trying to apply if-else condition over a dataframe.

If the ACCOUNT_LOGIN contains any special character create the csv under a ErrorFolder. else create the csv under ValidatedDataFolder

But the below is also creating a file under ValidatedDataFolder alongwith ErrorFolder even . How to acheive this validation of creating csv in different folders based on if-else condition?

dataframe ACCOUNT_LOGIN : [ AL&L@WA, ANGLIND, ASIGAFU ]

ACTION : [ AL, AN, AS ]

Trans_Desc: [ JSW, JSW, JSW ]

def validate(rowsofdataframe):

filename="TestFileSpclCharOp.txt" 

if  rowsofdataframe['ACCOUNT_LOGIN_1'].strip().isalnum()==False : #if any row contains any spcl character
    output_error_file=os.path.join(errorPath,filename)
    rowsofdataframe.to_csv(output_error_file,index=False)
    val="Login contains special character.FAILED"
else :
   
    validated_Files=os.path.join(foramttedPath,filename)
    rowsofdataframe.to_csv(validated_Files,index=False)
    val="All validation passed" 
    
return val

dfwithcolumns['Status'] = dfwithcolumns.apply(validate, axis=1)
1

There are 1 answers

2
Abhilash Awasthi On
input_folder_path = '/home/inp'
correct_files_folder = '/home/correct'
error_files_folder = '/home/error'
# All the multiple input files name in a list
input_file_names = ['a.csv', 'b.csv', 'c.csv']

# Looping over all files
for f in input_file_names:
    # Reading each file into a dataframe
    df = pd.read_csv(os.path.join(input_folder_path, f))
    # Checking whether the column has any special characters or not. If it has one special character also then also it gets saved in error folder
    if df['ACCOUNT_LOGIN_1'].str.isalnum().sum()!=df.shape[0]:
        df.to_csv(os.path.join(error_files_folder, f), index=False)
    else:
        # No special character. Save in correct files folder
        df.to_csv(os.path.join(correct_files_folder, f), index=False)