简体   繁体   English

使用来自第二个数据框的值填充熊猫数据框,第二个数据框具有一些常见的行和列

[英]Filling a pandas dataframe with values from a second dataframe with some rows and columns in common

Let us suppose I have two pandas dataframes df1 and df2 让我们假设我有两个熊猫数据帧df1df2

df1
         s1   s2   s3
    bob  nan  nan  nan
    john nan  nan  nan
    matt nan  nan  nan

and

df2
         s1   s3   s4
    bob  32   11   22
    matt 1   nan    2

I would to fill df1 with values from df2 rows and columns which exist in df1 so that my output is 我想,以填补df1从值df2行和列存在于df1使我的输出是

     s1   s2   s3
bob  32   nan   11
john nan  nan  nan
matt 1    nan  nan

This means, in this toy case, I'm not interested in column s4 of df2 to fill df1 . 这意味着,在这种玩具情况下,我对df2 s4列不感兴趣以填充df1 All my attempt to use merge have sadly failed and I always end up with a dataframe with all nan . 我所有尝试使用merge尝试都不幸失败了,而我最终总是得到一个所有nan的数据框。

Inplace Operation 就地操作
Use pd.DataFrame.update 使用pd.DataFrame.update
This overwrites all positions in df1 where there was a non-null value in df2 这将覆盖所有的位置在df1那里有一个非空值df2

df1.update(df2)

df1

        s1  s2    s3
bob   32.0 NaN  11.0
john   NaN NaN   NaN
matt   1.0 NaN   NaN

Produce a Copy 1 出示副本1
Use pd.DataFrame.align , pd.DataFrame.fillna , and pd.DataFrame.reindex_like 使用pd.DataFrame.alignpd.DataFrame.fillnapd.DataFrame.reindex_like
fillna won't work unless the index and columns are aligned. 除非索引和列对齐,否则fillna无效。

pd.DataFrame.fillna(*df1.align(df2)).reindex_like(df1)

        s1  s2    s3
bob   32.0 NaN  11.0
john   NaN NaN   NaN
matt   1.0 NaN   NaN

Produce a Copy 2 制作副本2
pd.DataFrame.combine_first and pd.DataFrame.reindex_like pd.DataFrame.combine_firstpd.DataFrame.reindex_like
It's debatable which one you put first. 您首先争论哪个值得商de。 Considering df1 is all nan it doesn't really matter. 考虑到df1是所有nan它其实并不重要。 But this will keep any pre-existing non-null values in df1 . 但这会将所有预先存在的非null值保留在df1 Otherwise, you could switch the positions to df2.combine_first(df1) . 否则,您可以将位置切换到df2.combine_first(df1)

df1.combine_first(df2).reindex_like(df1)

        s1  s2    s3
bob   32.0 NaN  11.0
john   NaN NaN   NaN
matt   1.0 NaN   NaN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据相同 dataframe 列表中某些列的值从 pandas dataframe 中选择行? - Selecting rows from a pandas dataframe based on the values of some columns in a list of the same dataframe? 从行到 Pandas 数据帧的列的值 - Values from rows to columns of a pandas dataframe 在Pandas Python行中的数据框中填充None值 - Filling None values in the dataframe in rows Pandas Python 从Pandas Dataframe,在某些列中具有共同值的不同行之间构建networkx图表或流程图 - From a Pandas Dataframe, build networkx chart or flow chart between different rows with common values in certain columns 按Pandas中的数据框分组,列中包含常用值 - Group by a dataframe in Pandas with common values across columns 在Pandas数据框中的列中找到常用值 - Find the common values in columns in Pandas dataframe 从另一个数据框向Pandas数据框添加值 - Adding values to Pandas dataframe from a second dataframe 来自Pandas中Dataframe行的Dataframe列 - Dataframe columns from Dataframe rows in Pandas How to filter the rows of a dataframe based on the presence of the column values in a separate dataframe and append columns from the second dataframe - How to filter the rows of a dataframe based on the presence of the column values in a separate dataframe and append columns from the second dataframe 熊猫-用另一个数据框的值填充一个数据框的每一行 - Pandas - Filling each rows of one Dataframe with value from another Dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM