Pandas df.apply似乎会导致意想不到的结果

i1icjdpr  于 2023-05-15  发布在  其他
关注(0)|答案(1)|浏览(132)

我有一段这样的代码:

import numpy as np
df111=[]
def getNoNaN(row_in,dflist):
    if np.sum(~pd.isna(row_in[3:])) > 1:
        dflist.append(row_in)
dftest.apply(axis=1,func=getNoNaN,dflist=df111)
df111[0]

并且,在df 111的第一行中得到了一个意外的结果。

df111[0]
Name     d4a0dad668a4249f8ddb8cfd336e3397
ID       f703f7b7e0173bc4269bfff2d8882439
Level                                   8
No.3                                  NaN
No.4                                  NaN
No.5                                  NaN
No.6                                  NaN
No.7                                  NaN
No.8                                  NaN
No.9                                  NaN
No.10                                 NaN
No.11                                 NaN
No.12                                 NaN
No.13                                 NaN
No.14                             0.55456
Name: 3615, dtype: object

这看起来很不寻常,因为np.sum(~pd.isna(row_in[3:]))等于1,我不太清楚为什么它会出现在df 111列表中。
值得注意的是,我现在有多种解决方案,可以正常实现类似的功能(并且非常清楚上述代码的不恰当之处)。
但是这个现象仍然困扰着我,也许我错过了Pandas文档中的一些重要提示?希望能得到您的帮助。
可用的测试数据可在此处下载:https://drive.google.com/file/d/1AuylSty8-8jmgZQE9_nY2cSYeEs1aw5v/view?usp=share_link

0sgqnhkj

0sgqnhkj1#

你不需要使用apply,你可以使用向量化代码:

df = pd.read_csv('apply_test_data.csv', index_col=0)
out = df[df.iloc[:, 3:].notna().sum(axis=1) > 1]

输出:

>>> out
                                  Name                                ID  Level  ...     No.12  No.13     No.14
0     ac64934249131b017d85de7b17556ebe  a015c9f38e2ebe6f900ed808119e4c2c      4  ...  0.232793    NaN       NaN
4     3b3d41ddd2c029057987db03c86cb351  43dd1452a809337189cfb0e32f3bc0da      4  ...  0.041589    NaN       NaN
7     5873a3f324dac3c389e0e1c570fe0b65  f34d7b40bf2848ab3f26390a14ece18e      5  ...  0.034054    NaN       NaN
10    a7b105839ac6343847216b21b391e1eb  7bfe373a31b9af37b6db07bbe17e113c      2  ...  0.285993    NaN       NaN
12    f0851646a101642c2cfa0d3a166104c8  e64f2e38c37509f4cb027fd77421a586      6  ...  0.101971    NaN       NaN
...                                ...                               ...    ...  ...       ...    ...       ...
3201  304507b189dd1c79ac9fdf88f7a12789  59fa7b6d602d9d4df4f4bba3750d9108     10  ...       NaN    NaN  0.519524
3218  fda0dabb9548ea30f824daab7d10b3d1  05d9b1e13f568b108306133d299598ad      7  ...       NaN    NaN  0.000820
3226  328d3ce95d79445f6885b2274549662d  b23f8565d14733bcda065add4987074c      3  ...       NaN    NaN  0.534249
3227  9c80e58c3308ddd40a9b1a8f59a09e3c  c86bf81b97bed0c099062910a0282b13      6  ...       NaN    NaN  0.000830
3243  ae9e52e41df532d1feea03f9ae0825fb  8b69d03591547968906c37a78dd81d51      2  ...  0.320757    NaN  0.022925

[1591 rows x 15 columns]

关于您的错误:为了解决这个问题,你必须在每次在列表中追加一行(系列)时制作一个副本:

df111 = []
def getNoNaN(row_in,dflist):
    if np.sum(~pd.isna(row_in[3:])) > 1:
        # print(id(row_in))  # Uncomment to check the memory address of row_in
        dflist.append(row_in.copy())  # HERE
dftest.apply(axis=1, func=getNoNaN, dflist=df111)

输出:

>>> df111[0]
Name     ac64934249131b017d85de7b17556ebe
ID       a015c9f38e2ebe6f900ed808119e4c2c
Level                                   4
No.3                                  0.0
No.4                                  NaN
No.5                                  NaN
No.6                                  NaN
No.7                                  NaN
No.8                                  NaN
No.9                                  0.0
No.10                                 NaN
No.11                                 NaN
No.12                            0.232793
No.13                                 NaN
No.14                                 NaN
Name: 0, dtype: object

相关问题