python - 比较并选择第一个非空列值 Python Dataframe
问题描述
只比较 3 列,homePhone、workPhone、CellPhone、
如何在“我想要”列中选择第一个非空列值,
使用 Python 熊猫
name homePhone workPhone CellPhone I want
1 Tom 888 666 null 888
2 John null 777 null 777
3 Lily null null 333 333
解决方案
如果此事确实只涉及三列,则可以使用np.where
NumPy 中的:
import numpy as np
df['I want'] = np.where(df['homePhone'].isnull(),
np.where(df['workPhone'].isnull(),
df['CellPhone'],
df['workPhone']),
df['homePhone']).astype(int)
推荐阅读
- android - 无法解决:com.android.databinding:library:3.1.2
- windows - 验证用户输入是几个允许的单词之一
- android - 使用其 USB id 在特定设备上运行 CTS
- android -
- sql - 具有可变参数 varchar 输入和可变参数整数输出的 SQL 函数
- c# - 如何使用 ASP WebForms 将日志文件流式传输到网页
- java - 当我们只有元素的标题时,如何获取某个列表元素的属性?
- css - 在调整大小时将 svg “粘贴”到元素的顶部
- php - 在 RHEL 7 中找不到 php-fpm PDO 驱动程序 mysql
- javascript - JavaScript 将许多函数括起来