美文网首页
转载:pandas实现相同表格的互补型汇总

转载:pandas实现相同表格的互补型汇总

作者: 火炬广场老大爷 | 来源:发表于2021-02-25 15:52 被阅读0次

使用combine-first函数或update实现

df1 = pd.DataFrame([[np.nan, 3, 5], [4, np.nan, np.nan],[np.nan, 6, np.nan]])
df2 = pd.DataFrame([[7, np.nan, 8], [9, 10, 666]],index=[1, 2])
print(df1)
print('--------------------------------')
print(df2)
print('--------------------------------')

# 通过combine_first来讲df2填充到df1里空值的位置上,并且是根据index来做的。
print(df1.combine_first(df2))
print('--------------------------------')

# 如果df3的index比df1的多,那么就直接更新到df1上。
df3 = pd.DataFrame([[7, np.nan, 8], [9, 10, 666]],index=['a',1])
print(df1.combine_first(df3))
print('--------------------------------')

# update,根据index的位置,将相同位置的数值用df2直接覆盖df1。
df1.update(df2)
print(df1)
print('--------------------------------')

运行结果:

     0    1    2
0  NaN  3.0  5.0
1  4.0  NaN  NaN
2  NaN  6.0  NaN
--------------------------------
   0     1    2
1  7   NaN    8
2  9  10.0  666
--------------------------------
     0    1      2
0  NaN  3.0    5.0
1  4.0  NaN    8.0
2  9.0  6.0  666.0
--------------------------------
     0     1      2
0  NaN   3.0    5.0
1  4.0  10.0  666.0
2  NaN   6.0    NaN
a  7.0   NaN    8.0
--------------------------------
     0     1      2
0  NaN   3.0    5.0
1  7.0   NaN    8.0
2  9.0  10.0  666.0
--------------------------------

原文地址:https://blog.csdn.net/MicroWisdom/article/details/85881054?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.control&dist_request_id=8c513551-f328-4257-bfe1-6212a26ddc8a&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.control

相关文章

网友评论

      本文标题:转载:pandas实现相同表格的互补型汇总

      本文链接:https://www.haomeiwen.com/subject/llrufltx.html