2 回答

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊
我不確定我的問題是否正確,但至少這個(gè)答案會(huì)幫助其他人給你答案如果這不是你要找的,請(qǐng)給我機(jī)會(huì)
import pandas as pd
#your dataframe
maindf = {'PM Owner': ['A', 'B','C','A','E','F'], 'Risk Tier': [1,3,1,1,1,2],'sam' :['A0','B0','C0','D0','E0','F0']}
Maindf = pd.DataFrame(data=maindf)
#what you are looking for
filterdf = {'PM Owner': ['A' ], 'Risk Tier': [ 1 ]}
Filterdf = pd.DataFrame(data=filterdf)
#Filtering
NewMaindf= (Maindf[Maindf[['PM Owner','Risk Tier']].astype(str).sum(axis = 1).isin(
Filterdf[['PM Owner','Risk Tier']].astype(str).sum(axis = 1))])
#Just one sample
print( (NewMaindf).sample())
#whole dataset after filtering
print( (NewMaindf) )
結(jié)果 :
PM Owner Risk Tier sam
3 A 1 D0
PM Owner Risk Tier sam
0 A 1 A0
3 A 1 D0

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
您指定的條件可以匹配許多行,這就是您收到錯(cuò)誤的原因,同時(shí)建議您使用將結(jié)果減少為單個(gè)值的函數(shù)之一。然而,目前形式的條件實(shí)際上可以用作掩模,因此只需縮小范圍即可抽取符合標(biāo)準(zhǔn)的樣本,即:
df.loc[(df['PM Owner'] == 'Alex') & (df['Risk Tier'] == 'Critical'), 'Risk Tier'].sample()
如果您需要循環(huán)遍歷每個(gè) PM 所有者,您可以這樣做:
for pm_owner in df['PM Owner']: sample = df.loc[(df['PM Owner'] == pm_owner) & (df['Risk Tier'] == 'Critical'), 'Risk Tier'].sample()
添加回答
舉報(bào)