Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在pandas/python中使用空值执行条件语句_Python_Pandas_Numpy_If Statement_Null - Fatal编程技术网

如何在pandas/python中使用空值执行条件语句

如何在pandas/python中使用空值执行条件语句,python,pandas,numpy,if-statement,null,Python,Pandas,Numpy,If Statement,Null,如何在熊猫身上进行条件替换 df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]]) 在R中,我认为这段代码很容易理解: library(dplyr) df = df %>% mutate( # mutate means create new column for non-r people my_new_column = ifelse( is.na(the_2nd_column)==TRUE & i

如何在熊猫身上进行条件替换

df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]])
在R中,我认为这段代码很容易理解:

library(dplyr)
df = df %>% 
mutate(   #   mutate means create new column for non-r people
my_new_column = ifelse( is.na(the_2nd_column)==TRUE & is.na(the_3rd_column)==TRUE, ' abc', 'cuz')
我如何在熊猫中做到这一点-可能是语法上的愚蠢问题,但我听过np。R中if-else的等价物在哪里

df['new_column'] = np.where(np.nan(....help here with a conditional....))

np.where
像这样

df['new_column'] = np.where(df[1].isnull() & df[2].isnull(), 'abc', 'cuz')
print(df)

或者用更多的numpy更快

df['new_column'] = \
    np.where(np.isnan(df[1].values) & np.isnan(df[2].values), 'abc', 'cuz')


     0    1    2 new_column
0  1.0  2.0  3.0        cuz
1  4.0  NaN  NaN        abc
2  NaN  NaN  9.0        cuz

定时


使用
np.where

In [279]: df['new'] = np.where(df[[1, 2]].isnull().all(axis=1), 'abc', 'cuz')

In [280]: df
Out[280]:
     0    1    2  new
0  1.0  2.0  3.0  cuz
1  4.0  NaN  NaN  abc
2  NaN  NaN  9.0  cuz

你能把时间按升序排列吗?中间有一个208us,应该上升D:P HeheThank你认为这是有效的-尤其是这个是空的。。。。。不过,如果你不介意的话,我有个问题。如果我是两个,则将两个数据集以及1个数据集中存在的1列附加到另一个数据集中不存在的数据集中-is nan版本会在is null版本工作时产生错误消息。似乎是null对错误更具鲁棒性。也许有一个更大数据集的基准测试会更好吗?