gpt4 book ai didi

python - 查找索引行和数据框中每一行之间的公共(public)非空列

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:51 25 4
gpt4 key购买 nike

我想找出索引行和数据框之间共有的非空列的百分比。例如:

df(索引) =

A   B   C   D   E   F
3 NaN 4 NaN 5 6

和 df1 =

A   B   C   D   E   F
1 3 5 NaN 7 NaN
NaN 2 4 NaN 3 2
7 6 NaN 5 2 NaN
2 NaN 5 NaN 7 3

我正在寻找非空公共(public)列的百分比。所以理想的结果是:

    A   B   C   D   E   F   Common%
1 3 5 NaN 7 NaN 75
NaN 2 4 NaN 3 2 75
7 6 NaN 5 2 NaN 50
2 NaN 5 NaN 7 3 100

提前致谢

最佳答案

你可以这样做:

  # create mask for non-nan values
v = ~np.isnan(df)
w = ~np.isnan(df1)

# count non zeros of intersection
a = np.count_nonzero(np.logical_and(w, v), axis=1)

# count non zeros of v (number of non-nan values in df)
b = np.count_nonzero(v)

# find common percentage
print((a / b) * 100)

输出

[ 75.  75.  50. 100.]

您可以像这样添加新列:

df1['common'] = (a / b) * 100
print(df1)

输出

     A    B    C    D  E    F  common
0 1.0 3.0 5.0 NaN 7 NaN 75.0
1 NaN 2.0 4.0 NaN 3 2.0 75.0
2 7.0 6.0 NaN 5.0 2 NaN 50.0
3 2.0 NaN 5.0 NaN 7 3.0 100.0

关于python - 查找索引行和数据框中每一行之间的公共(public)非空列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54553581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com