第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 中列表理解的多個條件

Python 中列表理解的多個條件

紫衣仙女 2023-09-26 16:57:53
我想根據(jù)索引中的值創(chuàng)建一個列:如果索引以字母值開頭而不是 '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...可重現(xiàn)的例子: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秒到達戰(zhàn)場

TA貢獻1828條經(jīng)驗 獲得超6個贊

非循環(huán)解決方案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


查看完整回答
反對 回復(fù) 2023-09-26
?
翻閱古今

TA貢獻1780條經(jīng)驗 獲得超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]



查看完整回答
反對 回復(fù) 2023-09-26
  • 2 回答
  • 0 關(guān)注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號