Skip to content

Instantly share code, notes, and snippets.

@raprasad
Created April 26, 2018 19:25
Show Gist options
  • Save raprasad/07ef09cc2427815a1384a4fd965c1105 to your computer and use it in GitHub Desktop.
Save raprasad/07ef09cc2427815a1384a4fd965c1105 to your computer and use it in GitHub Desktop.
get dataframe
def get_dataframe(input_file, start_idx=0, num_rows=100, delimiter=None, is_excel=False):
"""Retrieve a pandas dataframe based on format"""
try:
if is_excel:
dataframe = pd.read_excel(input_file) # add nrows, skiprows + lineterminator, if needed
else:
dataframe = pd.read_csv(input_file,
delimiter=delimiter) # add nrows, skiprows + lineterminator, if needed
return ok_resp(dataframe)
except pd.errors.ParserError as err_obj:
err_msg = ('Failed to load csv file (pandas ParserError).'
'\n- File: %s'
'\n- %s') % \
(input_file, err_obj)
return err_resp(err_msg)
except PermissionError as err_obj:
err_msg = ('No read prermission on this file:'
'\n- File: %s'
'\n- %s') % \
(input_file, err_obj)
return err_resp(err_msg)
except Exception as err_obj:
err_msg = ('Failed to load file.'
'\n- File: %s'
'\n- %s') % \
(input_file, err_obj)
return err_resp(err_msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment