为了账号安全,请及时绑定邮箱和手机立即绑定

Python 中列表理解的多个条件

Python 中列表理解的多个条件

紫衣仙女 2023-09-26 16:57:53
我想根据索引中的值创建一个列:如果索引以字母值开头而不是 'I0',则返回“P”,否则返回“C”。尝试过:df['new_col'] = ['P' if (x[0].isalpha() and not x[0].startswith("I0"))  else 'C' for x in df.index]但它返回以以下开头的行的“P” 'I0':         A           B           C       new_colIndex           I00001  1.325337    4.692308    1.615385    PI00002  1.614780    3.615385    0.769231    PI00003  1.141453    5.461538    2.000000    PI00004  0.918300    8.538462    2.769231    PI00005  1.189606    11.846154   2.692308    PI00006  0.941459    7.153846    2.153846    PI00007  0.466383    12.153846   9.384615    PI00008  0.308627    198.692308  23.461538   PI00011  0.537142    23.384615   6.846154    PI00012  1.217390    11.923077   1.230769    PI00013  1.052840    3.384615    2.000000    P...可重现的例子:df = pd.DataFrame({'A': {'I00001': 1.3253365856660808,  'I00002': 1.6147800817881086,  'I00003': 1.1414534979918203,  'I00004': 0.9183004454646491,  'I00005': 1.1896061362142527,  'I00006': 0.941459102789141,  'I00007': 0.46638312473267185,  'I00008': 0.3086270976042302,  'I00011': 0.5371419441302684,  'I00012': 1.2173904641254587,  'I00013': 1.052839529263679,  'I00014': 1.3587324409735149,  'I00015': 3.464101615137755,  'I00016': 1.1989578808281798,  'I00018': 0.2433560755649686,  'I00019': 0.5510000980337852,  'I00020': 3.464101615137755,  'I00022': 1.0454523047666737,  'I00023': 1.3850513878332371,  'I00024': 1.3314720972390754}, 'B': {'I00001': 4.6923076923076925,  'I00002': 3.6153846153846154,  'I00003': 5.461538461538462,  'I00004': 8.538461538461538,  'I00005': 11.846153846153847,  'I00006': 7.153846153846154,  'I00007': 12.153846153846153,  'I00008': 198.69230769230768,  'I00011': 23.384615384615383,  'I00012': 11.923076923076923,  'I00013': 3.3846153846153846,  'I00014': 1.0,)
查看完整描述

2 回答

?
30秒到达战场

TA贡献1828条经验 获得超6个赞

非循环解决方案numpy.where

df['new_col'] = np.where(df.index.str[0].str.isalpha() &

                         ~df.index.str.startswith("I0"), 'P', 'C')

您的解决方案 -x[0]从中删除x[0].startswith("I0")- 它测试第一个值,如果不是I0,则始终是True:


df['new_col'] = ['P' if (x[0].isalpha() and not x.startswith("I0"))   

                     else 'C' for x in df.index]

测试:


df = pd.DataFrame({'A': {'AA00001': 1.3253365856660808,

  'I00002': 1.6147800817881086,

  'IR0003': 1.1414534979918203,

  '00004': 0.9183004454646491,

  '**00005': 1.1896061362142527,

  'I00007': 0.46638312473267185}}

)


df['new_col'] = np.where(df.index.str[0].str.isalpha() &

                         ~df.index.str.startswith("I0"), 'P', 'C')


df['new_col1'] = ['P' if (x[0].isalpha() and not x.startswith("I0"))   

                      else 'C' for x in df.index]

print (df)

                A new_col new_col1

**00005  1.189606       C        C

00004    0.918300       C        C

AA00001  1.325337       P        P

I00002   1.614780       C        C

I00007   0.466383       C        C

IR0003   1.141453       P        P


查看完整回答
反对 回复 2023-09-26
?
翻阅古今

TA贡献1780条经验 获得超5个赞

您正在检查x[0].startswith("I0")您的代码,这是不正确的尝试这个(检查x.startswith("I0")

df['new_col'] = ['P' if (x[0].isalpha() and not x.startswith("I0"))  else 'C' for x in df.index]



查看完整回答
反对 回复 2023-09-26
  • 2 回答
  • 0 关注
  • 62 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信