鱼C论坛

 找回密码
 立即注册
查看: 1672|回复: 1

python where函数求助

[复制链接]
发表于 2021-1-12 10:38:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import pandas as pd
from pandas import Series, DataFrame
import numpy as np

a = pd.Series([np.nan, 2.5, 0.0, 3.5, 4.5, np.nan],
              index = ['f', 'e', 'd', 'c', 'b', 'a'])
b = pd.Series([0, np.nan, 2., np.nan, np.nan, 5.],
              index = ['a', 'b', 'c', 'd', 'e', 'f'])

a
Out[11]:
f    NaN
e    2.5
d    0.0
c    3.5
b    4.5
a    NaN
dtype: float64

b
Out[12]:
a    0.0
b    NaN
c    2.0
d    NaN
e    NaN
f    5.0
dtype: float64

np.where(pd.isnull(a), b, a)
Out[13]: array([0. , 2.5, 0. , 3.5, 4.5, 5. ])

np.where(pd.isnull(b), b, a)
Out[14]: array([nan, nan,  0., nan, nan, nan])


谁能解释一下np.where(pd.isnull(a), b, a) 和 np.where(pd.isnull(b), b, a)  是什么意思啊?谢谢。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-13 02:13:29 | 显示全部楼层
np.where(pd.isnull(a), b, a)--->a数据有缺失,输出b,否则输出a

搜索引擎搜索下
pandas的isnull()函数和numpy的where()函数就明白了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 14:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表