[英]Assigning many values to one variable
I am trying to get phone numbers based on users choice我正在尝试根据用户的选择获取电话号码
#the dict that contains the data I need
x={"contact":
{
"facility_message": "testing testing testing",
"facilitydigits":101,
"name": "",
"urn": "tel:+1234567891011",
"uuid": "60409852-a2089-43d5-bd4c-4b89a6191793",
"selection_anc_pnc":"C"
}
}
#extracting data from the dict
facility_number=str(x['contact']['facilitydigits'])
group=(x['contact']['selection_anc_pnc']).upper()
facility_message=(x['contact']['facility_message'])
#checking user selection
if group =='A':
group="MIMBA"
elif group =='B':
group='MAMA'
elif group=='C':
group='MAMA' and "MIMBA"
My df looks like so我的df看起来像这样
phone group County PNC/ANC Facility Name Optedout Facility Code
25470000040 MIMBA Orange PNC Centre FALSE 101
25470000030 MAMA Orange PNC Centre FALSE 101
25470000010 MIMBA Orange PNC Centre FALSE 101
25470000020 MAMA Orange PNC Centre FALSE 101
25470000050 MAMA Orange PNC Main Centre FALSE 112
extracting phone numbers from my df从我的df中提取电话号码
phone_numbers =merged_df.loc[(merged_df['Facility Code'] ==facility_number) & (merged_df['group'] == group) & (merged_df['Opted out'] == optout)]['phone']
print(phone_numbers)
what is currently happening because of the if statement由于 if 语句,当前正在发生什么
[25470000010,25470000040]
desired output所需 output
[25470000040,25470000030,25470000010,25470000020]
You are incorrectly assigning the group value using group = 'MAMA' and "MIMBA"
which after execution assigns the value "MIMBA"
to group which is the last truty value, instead what you want to do is to assign a list of values that group can take using group = ['MAMA', "MIMBA"]
.您使用group = 'MAMA' and "MIMBA"
错误地分配了组值,执行后将值"MIMBA"
分配给作为最后一个真实值的组,而不是您想要做的是分配该组的值列表可以使用group = ['MAMA', "MIMBA"]
。 Then, you can use Series.isin
method to filter the group in dataframe which belong to groups present in group
variable.然后,您可以使用Series.isin
方法过滤 dataframe 中属于group
变量中存在的组的组。
Use:利用:
if group =='A':
group=["MIMBA"]
elif group =='B':
group=['MAMA']
elif group=='C':
group=['MAMA', "MIMBA"]
m = (
merged_df['Facility Code'].astype(str).eq(facility_number)
& merged_df['group'].isin(group)
& merged_df['Optedout'].eq(optout)
)
phone_numbers = merged_df.loc[m, "phone"]
print(phone_numbers.values)
This prints:这打印:
[25470000040 25470000030 25470000010 25470000020] # assuming variable optout is False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.