amend_col= input("Would u like to 'remove' any column?:")
# Maybe check case or input type
if amend_col == "yes":
s_col= input("Select the column you want to add or remove:")
# Again maybe check case, put all the col ids to uppercase
try:
df.drop([s_col],axis=1)
print("Column {} has been found and removed.".format(s_col))
except:
print("Column {} does not exist.\nNo columns have been removed.".format(s_col))
else:
print("No columns removed.")
You can move your code in to a try, except
replacing if s_col in df.columns and amend_col =="yes": ...
. Firstly, you dont need to check and amend_col =="yes"
as it was checked in the previous if
statement. Secondly, the try, except
performs the same function and provides feedback.
You could also do it with an if:
amend_col= input(" would u like to 'remove' any column?:")
if amend_col == "yes":
s_col= input("Select the column you want to add or remove:")
if s_col in df.columns:
print("Column is found and removed")
df.drop([s_col],axis=1)
else:
print("Column was not found")
else:
print("No columns removed")