【问题标题】:How to treat NaN or non aligned values as 1s or 0s in multiplying pandas DataFrames如何在乘法熊猫数据帧中将 NaN 或非对齐值视为 1 或 0
【发布时间】:2018-01-13 23:54:20
【问题描述】:

我想将未对齐或缺失的(NaN、Inf、-Inf)值视为 1 或 0。

df1 = pd.DataFrame({"x":[1, 2, 3, 4, 5], 
    "y":[3, 4, 5, 6, 7]}, 
    index=['a', 'b', 'c', 'd', 'e'])

df2 = pd.DataFrame({"y":[1, NaN, 3, 4, 5], 
    "z":[3, 4, 5, 6, 7]}, 
    index=['b', 'c', 'd', 'e', 'f'])

上面的代码结果如下

df1 * df2
    x     y   z
a NaN   NaN NaN
b NaN   4.0 NaN
c NaN   NaN NaN
d NaN  18.0 NaN
e NaN  28.0 NaN
f NaN   NaN NaN

我想忽略 NaN,并将未对齐的值视为左侧或右侧 DF 或两者中的 1。

例如

案例 1:用 1 替换 df1 中缺失或未对齐的值

df1 * df2
    x     y   z
a   1     3 NaN
b   2   4.0 NaN
c   3     5 NaN
d   4  18.0 NaN
e   5  28.0 NaN
f NaN   NaN NaN

案例 2:用 1 替换 df2 中缺失或未对齐的值

df1 * df2
    x     y   z
a NaN   NaN NaN
b NaN   4.0   3
c NaN   NaN   4
d NaN  18.0   5
e NaN  28.0   6
f NaN     5   7

案例 3:如果另一个 DF 中有值,则将任何缺失或未对齐的值替换为 1。

df1 * df2
    x     y   z
a   1     3 NaN
b   2   4.0   3
c   3     5   4
d   4  18.0   5
e   5  28.0   6
f NaN     5   7

在 addison 的情况下,我想将缺失或未对齐的值视为 0。

【问题讨论】:

    标签: python pandas dataframe indexing missing-data


    【解决方案1】:

    我认为您需要DataFrame.mul 和fillna 或combine_first 解决方案1 和2:

    print (df1.mul(df2).fillna(df1))
         x     y   z
    a  1.0   3.0 NaN
    b  2.0   4.0 NaN
    c  3.0   5.0 NaN
    d  4.0  18.0 NaN
    e  5.0  28.0 NaN
    f  NaN   NaN NaN
    
    print (df1.mul(df2).combine_first(df1))
         x     y   z
    a  1.0   3.0 NaN
    b  2.0   4.0 NaN
    c  3.0   5.0 NaN
    d  4.0  18.0 NaN
    e  5.0  28.0 NaN
    f  NaN   NaN NaN
    

    print (df1.mul(df2).fillna(df2))
        x     y    z
    a NaN   NaN  NaN
    b NaN   4.0  3.0
    c NaN   NaN  4.0
    d NaN  18.0  5.0
    e NaN  28.0  6.0
    f NaN   5.0  7.0
    
    print (df1.mul(df2).combine_first(df2))
        x     y    z
    a NaN   NaN  NaN
    b NaN   4.0  3.0
    c NaN   NaN  4.0
    d NaN  18.0  5.0
    e NaN  28.0  6.0
    f NaN   5.0  7.0
    

    在DataFrame.mul 中使用fill_value=1 解决3 输出:

    print (df1.mul(df2, fill_value=1))
         x     y    z
    a  1.0   3.0  NaN
    b  2.0   4.0  3.0
    c  3.0   5.0  4.0
    d  4.0  18.0  5.0
    e  5.0  28.0  6.0
    f  NaN   5.0  7.0
    

    【讨论】:

    • 这两个哪个更快? fillna 或 combine_first?
    • 难题,我相信 combine_first。有时可以交换,但有时不能。
    【解决方案2】:

    案例 1 用 1 替换 df1 中缺失或未对齐的值

    >>> df1.reindex(index=df1.index.union(df2.index), 
                    columns=df1.columns.union(df2.columns)).fillna(1)
       x  y  z
    a  1  3  1
    b  2  4  1
    c  3  5  1
    d  4  6  1
    e  5  7  1
    f  1  1  1
    

    如果需要,在上面的 sn-p 后面加上 .mul(df2)。

    案例 2 将 df2 中缺失或未对齐的值替换为 1

    >>> df2.reindex(index=df2.index.union(df1.index), 
                    columns=df2.columns.union(df1.columns)).fillna(1)
       x  y  z
    a  1  1  1
    b  1  1  3
    c  1  1  4
    d  1  3  5
    e  1  4  6
    f  1  5  7
    

    如果需要,在上面的 sn-p 后面加上 .mul(df1)。

    案例 3 如果另一个 DF 中有值,则将任何缺失或未对齐的值替换为 1。

    >>> df1.mul(df2).combine_first(df1).combine_first(df2)
        x   y   z
    a   1   3 NaN
    b   2   4   3
    c   3   5   4
    d   4  18   5
    e   5  28   6
    f NaN   5   7
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 2018-10-08
      • 2017-12-19
      • 2016-01-03
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      相关资源
      最近更新 更多