pandas 从另一个数据框中填充数据框中的缺失值 [英] Pandas fill missing values in dataframe from another dataframe
问题描述
我找不到熊猫函数(我以前见过),无法用另一个数据帧(假设可以指定一个公共索引)中的值替换一个数据帧中的NaN.有帮助吗?
I cannot find a pandas function (which I had seen before) to substitute the NaN's in a dataframe with values from another dataframe (assuming a common index which can be specified). Any help?
推荐答案
如果您有两个形状相同的数据框,则:
If you have two DataFrames of the same shape, then:
df[df.isnull()] = d2
可以解决问题.
只有df.isnull()
评估为True
(绿色突出显示)的位置才有资格进行分配.
Only locations where df.isnull()
evaluates to True
(highlighted in green) will be eligible for assignment.
实际上,DataFrame的大小/形状并不总是相同的,并且转换方法(尤其是.shift()
)很有用.
In practice, the DataFrames aren't always the same size / shape, and transforming methods (especially .shift()
) are useful.
传入的数据总是很脏,不完整或不一致.该课程的标准杆.有大量的熊猫教程和相关食谱用于处理这些情况.
Data coming in is invariably dirty, incomplete, or inconsistent. Par for the course. There's a pretty extensive pandas tutorial and associated cookbook for dealing with these situations.
这篇关于 pandas 从另一个数据框中填充数据框中的缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!