使用USASPENDING.gov中公开可用的csv文件。能够从海军提取数据,但不知道正确的语法添加第二个筛选器以排除所有记录与Dollarsobligated = 0
。Python 3 Pandas按多个列值进行过滤/提取,包括<> 0
代码是:
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')
我得到那个说<>
是无效的语法错误。没有网络上的'不等于0'的例子。
在Python2'<>'是相当于'!='。 [在Python3中,'<>'被删除](https://docs.python.org/3.0/whatsnew/3.0.html#removed-syntax)。 – unutbu
很高兴知道,谢谢unutbu :) –