简体   繁体   English

Gapfill pandas dataframe 在另一个 dataframe 中使用同一列中的值

[英]Gapfill pandas dataframe using values from same column in another dataframe

   a1 a2  a3    b1
0  0  0  12   NaN
1  0  3  NaN  NaN
2  2  3  1    NaN

In the dataframe df above, I want to gap-fill column a3 and b1 using median of values from the same columns in another dataframe df2 with the same columns but different values.在上面的 dataframe df中,我想使用来自另一个 dataframe df2中具有相同列但值不同的相同列的值的中值来填充列a3b1

   a1  a2  a3  b1
0  0  0  2  6 
1  0  3  3  7
2  2  3  4  8

I tried this: df.fillna(df2.median(), inplace=True) , but I am not sure if it does what I want我试过这个: df.fillna(df2.median(), inplace=True) ,但我不确定它是否符合我的要求

I think it does what you want.我认为它可以满足您的需求。

The median of a3 in df2 is 3, the median of b1 in df2 is 7. df2a3的中位数是3, df2b1的中位数是7。

>>> df.fillna(df2.median())
   a1  a2    a3   b1
0   0   0  12.0  7.0
1   0   3   3.0  7.0
2   2   3   1.0  7.0

So it looks like good, no?所以看起来不错,不是吗?

暂无
暂无

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

相关问题 使用 pandas dataframe 中的值作为另一个的列名 - Using values in a pandas dataframe as column names for another 熊猫:从另一列修改数据框中的值 - pandas: modifying values in dataframe from another column pandas 在列值匹配时使用来自另一个数据帧的值更新数据帧 - pandas update a dataframe with values from another dataframe on the match of column values 从Pandas Dataframe中找到列中的唯一值,然后查看这些值在另一列中是否具有相同的值 - From Pandas Dataframe find unique values in column and see if those values have the same values in another column 来自 Pandas DataFrame 的 Select 行与另一个 DataFrame 中的列值完全相同 - Select rows from a Pandas DataFrame with exactly the same column values in another DataFrame 使用另一个 dataFrame 更改 Pandas dataFrame 中的列中的值 - Changing values in a column in a Pandas dataFrame by using another dataFrame 使用同一列中的值更新数据框中的一列 - Updating a column in a dataframe with values from the same column another dataframe 如果数据框中的另一列使用pandas匹配某个值,则从数据框中的列中减去值 - substract values from column in dataframe if another column in dataframe matches some value using pandas 使用来自另一个DataFrame的值将列有效地添加到Pandas DataFrame - Efficiently add column to Pandas DataFrame with values from another DataFrame Pandas:根据另一个数据框中的值更新数据框中的多列 - Pandas : Updating multiple column in a dataframe based on values from another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM