使用==,而不是is,来测试相等性
同样,使用!= 代替is not 表示不等式。
is 在 Python 中有特殊的含义。如果两个变量指向同一个对象,则返回True,而== 检查变量引用的对象是否相等。另见Is there a difference between == and is in Python?。
不要重复掩码计算
您正在创建的布尔掩码是您的逻辑中最昂贵的部分。这也是您希望避免手动重复的逻辑,因为您的第一个和第二个掩码彼此相反。因此,您可以使用bitwise inverse ~(“波浪号”)(也可通过operator.invert 访问)来否定现有掩码。
空字符串不同于空值
可以通过== '' 测试相等与空字符串,但相等与空值需要一个专门的方法:pd.Series.isnull。这是因为空值在 NumPy 数组中表示,这些数组由 Pandas、np.nan 和 np.nan != np.nan by design 使用。
如果你想用空值替换空字符串,你可以这样做:
df['medical_plan_id'] = df['medical_plan_id'].replace('', np.nan)
从概念上讲,缺失值为 null (np.nan) 而不是空字符串是有意义的。但与上述过程相反,即将空值转换为空字符串也是可以的:
df['medical_plan_id'] = df['medical_plan_id'].fillna('')
如果差异很重要,您需要了解您的数据并应用适当的逻辑。
半决赛解决方案
假设你确实有空值,计算一个布尔掩码及其逆:
mask = df['medical_plan_id'].isnull()
df1 = df[mask]
df2 = df[~mask]
最终解决方案:避免额外的变量
创建额外的变量是程序员应该避免的事情。在这种情况下,不需要创建两个新变量,您可以使用GroupBy 和dict 来给出一个数据帧字典,其中包含False (== 0) 和True (== 1) 键对应于你的面具:
dfs = dict(tuple(df.groupby(df['medical_plan_id'].isnull())))
那么dfs[0] 代表df2,dfs[1] 代表df1(另见this related answer)。上面的一个变种,你可以放弃字典构建,使用 Pandas GroupBy 方法:
dfs = df.groupby(df['medical_plan_id'].isnull())
dfs.get_group(0) # equivalent to dfs[0] from dict solution
dfs.get_group(1) # equivalent to dfs[1] from dict solution
示例
将以上所有内容付诸实践:
df = pd.DataFrame({'medical_plan_id': [np.nan, '', 2134, 4325, 6543, '', np.nan],
'values': [1, 2, 3, 4, 5, 6, 7]})
df['medical_plan_id'] = df['medical_plan_id'].replace('', np.nan)
dfs = dict(tuple(df.groupby(df['medical_plan_id'].isnull())))
print(dfs[0], dfs[1], sep='\n'*2)
medical_plan_id values
2 2134.0 3
3 4325.0 4
4 6543.0 5
medical_plan_id values
0 NaN 1
1 NaN 2
5 NaN 6
6 NaN 7