df.loc过滤不适用于None值

时间:2015-07-30 19:26:47

标签: python python-2.7 pandas

为什么当过滤器为 BEGIN TRANSACTION UPDATE VolumeLicenses SET TotalLicenses = TotalLicenses WHERE LicenseId = 901 IF ( SELECT COUNT(*) FROM VolumeLicenseUsers WHERE LicenseId = 901 ) < ( SELECT TotalLicenses FROM VolumeLicenses WHERE LicenseId = 901 ) BEGIN INSERT INTO VolumeLicenseUsers ( LicenseId, UserId ) VALUES ( 901, 'John' ) COMMIT TRANSACTION END ELSE BEGIN --Add your Error Handing here ROLLBACK TRANSACTION END ==无时,此过滤无效?我还注意到Project ID而不是is None返回== None

KeyError: False

输出:

import pandas as pd
df = pd.DataFrame(data = [['Project1', 'CT', 800], [None, 3, 1000], ['Project3', 'CA', 20]], columns=['Project ID', 'State', 'Cost'])

print df.loc[df['Project ID'] == 'Project1'].values
print df.loc[df['Project ID'] == None].values

2 个答案:

答案 0 :(得分:2)

您必须使用isnull

In [3]:

df[df['Project ID'].isnull()]
Out[3]:
  Project ID State  Cost
1       None     3  1000

或使用apply

In [5]:

df.loc[df['Project ID'].apply(lambda x: x is None)]
Out[5]:
  Project ID State  Cost
1       None     3  1000

答案 1 :(得分:0)

只是详细说明,它不起作用,因为pandas使用np.nan,并且:

print np.nan == np.nan   # False
print np.nan == None     # False
print np.isnan(np.nan)   # True