Pandas DataFrame combine_first() 方法

定义和用法

merge_first() 方法组合两个 DataFrame 对象,如果第一个 DataFrame 对象为 NULL 值,则使用第二个 DataFrame 中的值。

实例

合并两个 DataFrame,如果第一个 DataFrame 具有 NULL 值,则使用第二个 DataFrame 中的数据:

import pandas as pd

df1 = pd.DataFrame([[1, 2], [None, 4]])
df2 = pd.DataFrame([[5, 6], [7, 8]])

print(df1.combine_first(df2))

亲自试一试

语法

dataframe.combine_first(other)

参数

参数 描述
other 必需。DataFrame。

返回值

DataFrame 对象。