Need to execute the script with try and except condition under a for loop, Divided the dataframe into 2 parts and trying to execute the first part and In last want to append back both the dataframe and concat using python.
Script inside the loop is being executed but in last it gives the Error.
Code :
df = pd.read_excel(open(r'data.xlsx', 'rb'), sheet_name='sheet1')
df1 = df.loc[df['flag'] != 'Not feasible']
df = df.loc[df['flag'] == 'Not feasible'].copy()
for index, file in df..iterrows():
# Run your cleaning codes
try:
file['Joining_Date'][index] = pd.to_datetime(file['Joining_Date'], errors='coerce')
file['Joining_Date'][index] = file['Joining_Date'].dt.strftime('%Y-%m-%d')
except ValueError:
file['status'] = ValueError
try:
file['age'][index] = file['age'].replace('[^\d.]', '', regex=True).astype(float)
except ValueError:
file['status'] = ValueError
try:
file[['col4','col5']][index] = file[['col4','col5']].apply(lambda x: x.astype(str).str.replace('\W',''))
except ValueError:
file['status'] = ValueError
try:
file['Contact'][index] = file['Contact'].replace('[^\d.]', '', regex=True).astype(float)
except ValueError:
file['status'] = ValueError
file['flag'][index] = "feasible"
##Appending the values to dataframe and concat with the Non Executed data
df = df.apply(file, axis=1)
df = pd.concat([df, df1]).sort_index()
Getting error : when trying to use
df = df.apply(file, axis=1)
df = pd.concat([df, df1]).sort_index()
Error: TypeError: '<' not supported between instances of 'int' and 'str'
Need Suggestion How the above script can be processed