Python 3 Pandas Filter/Extract by multiple column values, including <> 0
Question: Working with a publicly available csv file from USASPENDING.gov. Able to
extract data from Navy but do not know the right syntax to add a second filter
to exclude all records with `Dollarsobligated = 0`.

Code is:

    
    
    import pandas as pd
    
    df = pd.read_csv("2016_DOD_Contracts_Full_20160915.csv")
    df.columns = [c.replace(' ','_') for c in df.columns]
    new_df = df[(df.mod_agency == '1700: DEPT OF THE NAVY') & (df.dollarsobligated <> 0)]
    
    # Export result to CSV
    new_df.to_csv('example15.csv')
    

I get an error that says `<>` is invalid syntax. No examples of 'does not
equal 0' on the web yet.


Answer: I think you need replace `<>` to `!=` in [`boolean
indexing`](http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-
indexing), because [in Python3, <> was
removed](https://docs.python.org/3.0/whatsnew/3.0.html#removed-syntax), thanks
[`unutbu`](http://stackoverflow.com/questions/39830890/python-3-pandas-filter-
extract-by-multiple-column-values-including-0#comment66951499_39830890).

Also you can use [`str.replace`](http://pandas.pydata.org/pandas-
docs/stable/generated/pandas.Series.str.replace.html):

    
    
    df.columns = df.columns.str.replace(' ','_')
    new_df = df[(df.mod_agency == '1700: DEPT OF THE NAVY') & (df.Dollarsobligated != 0)]
    

Sample:

    
    
    df = pd.DataFrame({'mod agency':['1700: DEPT OF THE NAVY',
                                     '1700: DEPT OF THE NAVY',
                                     '1800: DEPT OF THE NAVY'],
                       'Dollarsobligated':[1,0,0],
                       'C':[7,8,9]})
    
    print (df)
       C  Dollarsobligated              mod agency
    0  7                 1  1700: DEPT OF THE NAVY
    1  8                 0  1700: DEPT OF THE NAVY
    2  9                 0  1800: DEPT OF THE NAVY
    
    df.columns = df.columns.str.replace(' ','_')
    new_df = df[(df.mod_agency == '1700: DEPT OF THE NAVY') & (df.Dollarsobligated != 0)]
    
    print (new_df)
       C  Dollarsobligated              mod_agency
    0  7                 1  1700: DEPT OF THE NAVY
    

