Pandas/Python how to skip errors and goto the next line of code?

Please mind you, I'm new to Pandas/Python and I don't know what I'm doing.

I'm working with CSV files and I basically filter currencies. Every other day, the exported CSV file may contain or not contain certain currencies.

I have several such cells of codes--

AUDdf = df.loc[df['Currency'] == 'AUD']
AUDtable = pd.pivot_table(AUDdf,index=["Username"],values=["Amount"],aggfunc=np.sum)
AUDtable.loc['AUD Amounts Rejected Grand Total'] = (AUDdf['Amount'].sum())
AUDdesc = AUDdf['Amount'].describe()

When the CSV doesn't contain AUD, I get ValueError: cannot set a frame with no defined columns. What I'd like to produce is a function or an if statement or a loop that checks if the column contains AUD, and if it does, it runs the above code, and if it doesn't, it simply skips it and proceeds to the next line of code for the next currency.

Any idea how I can accomplish this?

Thanks in advance.

Upvotes: 1

Views: 4239

Answers (3)

Sandeep_Rao
Sandeep_Rao

Reputation: 101

1.Worst way to skip over the error/exception:

    try:
        <Your Code>
    except:
        pass

The above is probably the worst way because you want to know when an exception occur. using generic Except statements is bad practice because you want to avoid "catch em all" code. You want to be catching exceptions that you know how to handle. You want to know what specific exception occurred and you need to handle them on an exception-by-exception basis. Writing Generic except statements leads to missed bugs and tends to mislead while running the code to test.

  1. Slightly worse way to handle the exception:

    try:
       <Your Code>
    except Exception as e:
       <Some code to handle an exception>
    

    Still not optimal as it is still generic handling

  2. Average way to handle it for your case:

     try:
       <Your Code>
     except ValueError:
       <Some code to handle this exception>
    

Other suggestion - Much Better Ways to deal with this:

1.You can get a set of the available columns at run time and aggregate based on if 'AUD' is in the list.

2.Clean your data set

Upvotes: 2

jojo_040
jojo_040

Reputation: 132

This can be done in 2 ways:

  1. You can create a try and except statement, this will try and look for the given currency and if a ValueError occurs it will skip and move on:
try:
    AUDdf = df.loc[df['Currency'] == 'AUD']
    AUDtable = pd.pivot_table(AUDdf,index=["Username"],values["Amount"],aggfunc=np.sum)
    AUDtable.loc['AUD Amounts Rejected Grand Total'] = (AUDdf['Amount'].sum())
    AUDdesc = AUDdf['Amount'].describe()
except ValueError:
    pass
  1. You can create an if statement which looks for the currencies presence first:
currency_set = set(list(df['Currency'].values))

if 'AUD' in currency_set:
    AUDdf = df.loc[df['Currency'] == 'AUD']
    AUDtable = pd.pivot_table(AUDdf,index=["Username"],values=["Amount"],aggfunc=np.sum)
    AUDtable.loc['AUD Amounts Rejected Grand Total'] = (AUDdf['Amount'].sum())
    AUDdesc = AUDdf['Amount'].describe()

Upvotes: 2

jhanvi shah
jhanvi shah

Reputation: 11

You can use try and except where

try:
    #your code here
except:
    #some print statement 
    pass

Upvotes: 0

Related Questions