我有两个数据框,其中包含有关同一患者的不同信息。我需要使用数据框1来过滤数据框2,以便数据框2仅在df_1
,chromosome
,{ {1}}和strand
。如果df_1中有一个elementloc
值,我想将patient
放在同一位置的NaN
中。对于NaN
中已有的df_2
个值,我想将其保留为NaN。
因此,NaN
和df_2
像这样:
df_1
我希望新的df_2
看起来像:
df_1 = pd.DataFrame({'chromosome': [1, 1, 5, 4],
'strand': ['-', '-', '+', '-'],
'elementloc': [4991, 8870, 2703, 9674],
'Patient1_Reads': ['NaN', 25, 50, 'NaN'],
'Patient2_Reads': [35, 200, 'NaN', 500]})
print(df_1)
chromosome strand elementloc Patient1_Reads Patient2_Reads
0 1 - 4991 NaN 35
1 1 - 8870 25 200
2 5 + 2703 50 NaN
3 4 - 9674 NaN 500
df_2 = pd.DataFrame({'chromosome': [1, 1, 5, 4],
'strand': ['-', '-', '+', '-'],
'elementloc': [4991, 8870, 2703, 9674],
'Patient1_PSI': [0.76, 0.35, 0.04, 'NaN'],
'Patient2_PSI': [0.89, 0.15, 0.47, 0.32]})
print(df_2)
chromosome strand elementloc Patient1_PSI Patient2_PSI
0 1 - 4991 0.76 0.89
1 1 - 8870 0.35 0.15
2 5 + 2703 0.04 0.47
3 4 - 9674 NaN 0.32
答案 0 :(得分:2)
使用:
df3 = df1.merge(df2, on=['chromosome', 'strand', 'elementloc'])
r_cols = df3.columns[df3.columns.str.endswith('_Reads')]
p_cols = r_cols.str.strip('Reads') + 'PSI'
df3[p_cols] = df3[p_cols].mask(df3[r_cols].isna().to_numpy())
df3 = df3.drop(r_cols, 1)
详细信息:
步骤A:使用DataFrame.merge
创建通过合并df3
上的数据帧df1
和df2
获得的合并数据帧['chromosome', 'strand', 'elementloc']
。
# print(df3)
chromosome strand elementloc Patient1_Reads Patient2_Reads Patient1_PSI Patient2_PSI
0 1 - 4991 NaN 35.0 0.76 0.89
1 1 - 8870 25.0 200.0 0.35 0.15
2 5 + 2703 50.0 NaN 0.04 0.47
3 4 - 9674 NaN 500.0 NaN 0.32
步骤B:使用.str.endswith
来获取df3
结尾的_Reads
中的列,我们将其称为r_cols
列,然后使用此_Reads
列来获得相应的_PSI
列,我们将其称为p_cols
列。
# print(r_cols)
Index(['Patient1_Reads', 'Patient2_Reads'], dtype='object')
# print(p_cols)
Index(['Patient1_PSI', 'Patient2_PSI'], dtype='object')
步骤C:在_Reads
列上使用DataFrame.isna
获得布尔掩码,然后将此掩码与DataFrame.mask
一起使用以填充相应的{{ NaN
列中的1}}值。最后,使用DataFrame.drop
从合并的datframe _PSI
中删除_Reads
列以获得所需的结果:
df3