Pandas - 如果在 (col B) 中观察到列 (col A) 中的值,则使用来自 (col C) 的值创建列 (col D)

乔布森

如图所示,我有一个包含 2524 行和列的数据框 A。放在上下文中,这是一项基因组研究,其中状态 1 或 2 分别表示控制或生病。状态值与列 id 相关,例如 sample_1(索引 0)是病态,样本 5(索引 4)是对照。

          fid          iid       father       mother  sex  status
0        fam_7     sample_1            0            0    1       2
1     sample_2     sample_2            0            0    2       2
2     sample_3     sample_3            0            0    1       2
3     sample_4     sample_4            0            0    2       1
4       fam_34     sample_5            0            0    1       1

... ... ... ... ... ... 2519 fam_96 样本_2520 0 样本_1132 1 1 2520 fam_97 样本_2521 样本_760 0 1 2 2521 fam_98 样本_2522 样本_1452 0 2 2 293 样本2 5 2 5 am 293 样本2 1 5 fam_100 样本_2524 样本_2002 0 1 2

请注意,列父亲和母亲包含值 0。在这种情况下,这意味着他们没有父级,而是父级。

我想创建 2 个新列 ['Father status'] 和 ['mother status']。我想查找“父亲”列和“母亲”列中的值是否在列 id 中,然后具有状态。如您所见,在索引 2519 处,我们可以读取列 (mother) 中的 sample_1132。我希望添加该母亲的状态 ['母亲状态'],以确定一个孩子是否需要父母双方都生病。

为了更好地表示,我只为“孩子们”制作了一个单独的数据框:

       fid          iid       father       mother     sex  status
2426   fam_3  sample_2427  sample_1015  sample_1776    1       1
2427   fam_4  sample_2428  sample_1263  sample_1985    2       1
2428   fam_5  sample_2429   sample_517  sample_1508    1       1
2429   fam_6  sample_2430  sample_1753   sample_490    2       1
2430   fam_7  sample_2431     sample_1   sample_312    2       1
2432   fam_9  sample_2433  sample_1845  sample_1627    1       1
2434  fam_11  sample_2435   sample_574  sample_1682    2       1
2435  fam_12  sample_2436   sample_275   sample_947    2       1

2424   fam_1  sample_2425  sample_2397  sample_2351    1       2
2425   fam_2  sample_2426  sample_2063   sample_818    2       2
2431   fam_8  sample_2432   sample_239  sample_1151    2       2
2433  fam_10  sample_2434   sample_171   sample_747    2       2
2440  fam_17  sample_2441  sample_2042  sample_1540    2       2
2441  fam_18  sample_2442  sample_2182   sample_252    2       2
2444  fam_21  sample_2445  sample_1730  sample_1190    2       2
2448  fam_25  sample_2449  sample_1315   sample_762    1       2

我的预期输出将是这样的

       fid          iid       father       mother     sex  status  f_st  m_st 
2434  fam_11  sample_2435   sample_574  sample_1682    2       1     1     2
2435  fam_12  sample_2436   sample_275   sample_947    2       1     1     1  
2424   fam_1  sample_2425  sample_2397  sample_2351    1       2     2     2
2425   fam_2  sample_2426  sample_2063   sample_818    2       2     2     1
泽维尔·布特

我建议mother status通过合并获得该列

# Get the "status" column of the mother thanks to her index in the "id" column  
df = df.merge(df[["id", "status"]], left_on="mother", right_on="id", how="left", suffixes=('', '_y'))
# Drop the duplicated id column coming from the merge
df.drop("id_y", axis=1, inplace=True)
# Rename the status columns with the desired name
df.rename(columns={"status_y": "mother_status"}, inplace=True)

我让你处理mother_status列中的 NaN 值

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

熊猫-如果观察到另一列(col B)中的情况,则使用另一列(col A)的值创建一列(col C)

根据其他列的值计算 pandas 中的列(如果 col1<col2 和 col2 > col1,则 col3 = 1)

如果col b的值匹配,则用col a中的值填充col a的NULL。

在col C的某些值下,如何根据col A是否大于col B的平均值来改变新列?

对C列求和,除非col A中的值与col B相匹配

匹配时使用GAS搜索col A并返回col B的值

KDB - 如果不同的 col = 特定值,则将 col 乘以 -1

根据条件语句将条件列 C 设置为 Col A 或 Col B 的最快方法

如果col 1与col 3相同,如何用X替换col 4中的值,否则使用awk用col 2替换col 4中的每个条目

替换 df col-pandas 中的值

使用 pandas col 以 dict 格式存储 % 的值

如果 col A 和 B 上的值相同,则隐藏整行

PostgreSQL:在table_B的INSERT INTO table_A(col_b)中选择col_d table_B.col_c = table_A.col_a

PySpark DataFrame列参考:df.col与df ['col']与F.col('col')?

R //结合应用和R中的条件查找函数//在给定Col B和Col C满足要求的情况下求和col A中的值

如果col2的内容不在col1中,则串联2个chr列

C#Lambda-EF核心,Col A + Col B

选择配对行,Col Pandas 值

pandas dataframe groupby col如果在group by行中存在的值将其分配给groups行或从外部和增量分配最大值

如果它们的值在 col2 但不在 Python Pandas 的列表中,如何从 col1 中选择值?

在熊猫数据框中减去Col A-Col B

对于具有给定值的行,仅返回在Col B中找不到Col A值的行

如果表C中的Col A和Col B组合不存在,请在表3中插入新行

Pandas Dataframe - 按 Col A 分组并对每组 Col C 求和

Pandas:根据 col[B] 上的条件删除 col[A] 中的重复项,保留行

vba 提取和唯一到 Col B 併計數到 Col C

匹配来自 col2 中 col1 的 ID,如果该行的 col3 值小于在 col1 中匹配的行中的 col3 值,则取该行的值

根据col A行N和col B行N + 1的值之差插入行

在匹配col A之后找到col B中最接近的(较低)值