简体   繁体   English

2 个不同 dataframe 列中的比例

[英]Proportion in 2 different dataframe columns

After being treated with a first line of treatment that might be A,B,C(Drug code) what proportion of all cancer patients go on to be treated with a second line of treatment?在接受可能是 A、B、C(药物代码)的一线治疗后,在所有癌症患者 go 中接受二线治疗的比例是多少?

I am confused how to find that.我很困惑如何找到它。

How should I find that through for loop?我应该如何通过 for 循环找到它? Would anyone show me the code for this?有人会告诉我这个的代码吗? I am stuck there too.我也被困在那里。

I have tried implementing the code which i have pasted below我已经尝试实现我粘贴在下面的代码

for i in q1b['PatientID']:
#here every patient ID of dataset(q1b) should go through dataset2 patient drugcode
 dataset2['DrugCode']
 #Now i need to find proportion like if some patient was treating for A then if drug a didnt work how many patients went for B as asked in question.i tried running loop i think we need nested loop

EXPECTED OUTPUT:预期 OUTPUT:

AS I HAVE 29 PATIENTS IN DATASET(q1b) I want to finD Proportion like how many patients go for 2nd line of treatment from drug code A to B, Suppose there are 3 patients who went for drug code from A to B so proportion would be 3/29 * 100 like this way I expect the expected output.因为我在数据集中有 29 名患者(q1b)我想找到比例,比如有多少患者 go 进行第二线治疗,从药物代码 A 到 B,假设有 3 名患者从 A 到 B 的药物代码,所以比例将是3/29 * 100 像这样我期待预期的 output。 dataset picture数据集图片

So if I understand you correctly, you want to find the percentage of patients who have received drug A who have also received drug B. If so, you could do something like因此,如果我对您的理解正确,您想找到接受过药物 A 的患者同时接受过药物 B 的百分比。如果是这样,您可以执行类似的操作

codes = df.groupby('PatientID').apply(lambda x: set(x['DrugCode']))
codes[codes.apply(lambda x: 'A' in x)].apply(lambda x: 'B' in x).mean() * 100

If, moreover, you can assume that every patient starts out in 'A' , this boils down to此外,如果您可以假设每个患者从'A'开始,这归结为

df.groupby('PatientID').apply(lambda x: 'B' in x['DrugCode'].values).mean() * 100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM