python - 熊猫根据其他细胞的连续填充细胞
问题描述
我有df
很多缺失的数据,但基本上是相同的列(源自合并数据集)。例如,考虑以下情况:
temp = pd.DataFrame({"fruit_1": ["apple", "pear", "don't want to tell", np.nan, np.nan, np.nan],
"fruit_2": [np.nan, np.nan, "don't want to tell", "apple", "don't want to tell", np.nan],
"fruit_3": ["apple", np.nan, "pear", "don't want to tell", np.nan, "pear"]})
我现在想将它们合并为一列;冲突应按以下方式解决:
- np.nan 总是被其他信息覆盖
- “不想告诉”只覆盖 np.nan
- 任何其他值仅覆盖 np.nan 和“不想告诉”(即保留第一个值)。
我尝试创建一个新列并使用apply
(见下文)。
temp.insert(0, "fruit", np.nan)
temp['fruit'].apply(lambda row: row["fruit"] if np.isnan(row["fruit"]) and not np.isnan(row["fruit_1"]) else np.nan) # map col
但是,该代码会产生一个TypeError: 'float' object is not subscriptable
有人可以告诉我(1)这是否是一种普遍可行的方法 - 如果是这样,我的错误是什么?(2) 最有效的方法是什么?
提前非常感谢。
** 编辑 ** 预期的输出是
fruit
0 apple
1 pear
2 pear
3 apple
4 don't want to tell
5 pear
解决方案
有ffill
和附加np.where
s=temp.mask(temp=="don't want to tell").bfill(1).iloc[:,0]
s=np.where((temp=="don't want to tell").any(1)&s.isnull(),"don't want to tell",s)
s
Out[17]:
array(['apple', 'pear', 'pear', 'apple', "don't want to tell", 'pear'],
dtype=object)
temp['New']=s
temp
Out[19]:
fruit_1 ... New
0 apple ... apple
1 pear ... pear
2 don't want to tell ... pear
3 NaN ... apple
4 NaN ... don't want to tell
5 NaN ... pear
[6 rows x 4 columns]
推荐阅读
- python - 如何将 json 值传递给 highchart 热图
- html - Angular,如何为 iframe 源使用变量?
- python - 导致未处理错误的子进程错误
- excel - 如何以 $A$1 形式获取单元格的地址
- powershell - 如何从 Diagnostics-Performance 获取启动时间
- google-cloud-platform - 将 Filestack 与 GCP 存储桶集成是否需要 GCP 服务帐户的项目所有者角色?
- reactjs - 不可变的删除对象条目反应减速器
- mysql - 如何从 MySQL 中的变量中添加具有默认值的列?
- c# - 正则表达式替换 (C#)
- javascript - 通过访问变量的方式提高 Javascript 性能