如何使用条件遍历熊猫行以创建新列?

时间:2020-03-12 12:51:46

标签: python pandas

我有一个看起来像这样的数据框:

index   A   ... pheno
0      0.8        0
:
500000 1.4        5

其中pheno是介于0到5之间的int

我想遍历行并检查pheno列,然后根据pheno中的值创建一个新列,因此,如果pheno为0,则新列class将为1。 / p>

如此:

index   A  ...  pheno  class
0      0.8        0      1
:
500000 1.4        5      3

我知道如何使用df.iterrowsif/elif来执行此操作,但想知道是否有比为每个pheno值生成5条elif语句更快的方法?

只需添加-在某些情况下,我的pheno列最多可包含13个以上的值,因此为什么我认为if/elif效率低下。

0 个答案:

没有答案