pandas使用combine_first()方法:将第二个数据填充第一个缺失数据
import pandas as pd
import numpy as np
# 使用combine_first()方法为缺失数据填充
left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
'A': [np.nan, 'A0', 'A1', 'A2'],
'B': [np.nan, 'B1', np.nan, 'B2']})
right = pd.DataFrame({'A': ['C0', 'C1', 'C2'],
'B': ['D0', 'D1', 'D2']}, index=[1, 0, 2])
print(left)
print(right)
print(left.combine_first(right))
pandas----combine_first()
最新推荐文章于 2024-02-22 17:01:17 发布