Python 基于另一个数据帧中的行乘以数据帧中的行
我有一个数据帧df1Python 基于另一个数据帧中的行乘以数据帧中的行,python,pandas,conditional-statements,dataframe,multiplication,Python,Pandas,Conditional Statements,Dataframe,Multiplication,我有一个数据帧df1 Disease Gene1 Gene2 Gene3 Gene4 D1 0.1 1 26 1 D2 1 1 1 1 D3 1 18 0.5 17 D4 25 1 1 1 D5 1 1 1 1 D6 1
Disease Gene1 Gene2 Gene3 Gene4
D1 0.1 1 26 1
D2 1 1 1 1
D3 1 18 0.5 17
D4 25 1 1 1
D5 1 1 1 1
D6 1 33 1 12
D7 1 0.3 1 1
D8 5 1 1 1
此外,此数据帧为df2
Gene1 Gene2 Gene3 Gene4
0 0 1 0
1 0 1 1
1 1 0 0
1 0 1 0
0 1 1 1
0 0 1 0
0 1 0 0
0 0 1 0
我只想在df2中该基因存在1的情况下乘以df1中的值。请注意,df1中并非所有的值都是1,有些是小数。您可以将int值转换为布尔值,然后将其用作掩码:
In [30]:
df[df1.astype(bool)] * 10
Out[30]:
Disease Gene1 Gene2 Gene3 Gene4
0 NaN NaN NaN 260 NaN
1 NaN 10 NaN 10 10
2 NaN 10 180 NaN NaN
3 NaN 250 NaN 10 NaN
4 NaN NaN 10 10 10
5 NaN NaN NaN 10 NaN
6 NaN NaN 3 NaN NaN
7 NaN NaN NaN 10 NaN
如果要屏蔽“Disease”列,则只需使用其他df列来选择感兴趣的列:
In [34]:
cols = df1.columns
df[df1.astype(bool)][cols] * 10
Out[34]:
Gene1 Gene2 Gene3 Gene4
0 NaN NaN 260 NaN
1 10 NaN 10 10
2 10 180 NaN NaN
3 250 NaN 10 NaN
4 NaN 10 10 10
5 NaN NaN 10 NaN
6 NaN 3 NaN NaN
7 NaN NaN 10 NaN
编辑
从语义上讲,以下内容将处理您的其他df的值不是0
和1
:
In [36]:
cols = df1.columns
df[df1==1][cols]
Out[36]:
Gene1 Gene2 Gene3 Gene4
0 NaN NaN 26 NaN
1 1 NaN 1 1
2 1 18.0 NaN NaN
3 25 NaN 1 NaN
4 NaN 1.0 1 1
5 NaN NaN 1 NaN
6 NaN 0.3 NaN NaN
7 NaN NaN 1 NaN
可以将int值转换为布尔值,然后将其用作掩码:
In [30]:
df[df1.astype(bool)] * 10
Out[30]:
Disease Gene1 Gene2 Gene3 Gene4
0 NaN NaN NaN 260 NaN
1 NaN 10 NaN 10 10
2 NaN 10 180 NaN NaN
3 NaN 250 NaN 10 NaN
4 NaN NaN 10 10 10
5 NaN NaN NaN 10 NaN
6 NaN NaN 3 NaN NaN
7 NaN NaN NaN 10 NaN
如果要屏蔽“Disease”列,则只需使用其他df列来选择感兴趣的列:
In [34]:
cols = df1.columns
df[df1.astype(bool)][cols] * 10
Out[34]:
Gene1 Gene2 Gene3 Gene4
0 NaN NaN 260 NaN
1 10 NaN 10 10
2 10 180 NaN NaN
3 250 NaN 10 NaN
4 NaN 10 10 10
5 NaN NaN 10 NaN
6 NaN 3 NaN NaN
7 NaN NaN 10 NaN
编辑
从语义上讲,以下内容将处理您的其他df的值不是0
和1
:
In [36]:
cols = df1.columns
df[df1==1][cols]
Out[36]:
Gene1 Gene2 Gene3 Gene4
0 NaN NaN 26 NaN
1 1 NaN 1 1
2 1 18.0 NaN NaN
3 25 NaN 1 NaN
4 NaN 1.0 1 1
5 NaN NaN 1 NaN
6 NaN 0.3 NaN NaN
7 NaN NaN 1 NaN
我不知道您想要将
df1
乘以什么,但您可以使用以下方法:
>>> df1[df2 == 1] * 5
如果只想在df2
有1的地方乘以df1,可以执行以下操作:
>>> df1 * df2.where(df2 == 1, 5).where(df2 != 1, 1)
更新
In [51]: pd.concat([df1['Disease'], (df1 * df2).sum(axis=1)], axis=1)
Out[51]:
0 1
0 D1 26.0
1 D2 3.0
2 D3 19.0
3 D4 26.0
4 D5 3.0
5 D6 1.0
6 D7 0.3
7 D8 1.0
我不知道您想要将
df1
乘以什么,但您可以使用以下方法:
>>> df1[df2 == 1] * 5
如果只想在df2
有1的地方乘以df1,可以执行以下操作:
>>> df1 * df2.where(df2 == 1, 5).where(df2 != 1, 1)
更新
In [51]: pd.concat([df1['Disease'], (df1 * df2).sum(axis=1)], axis=1)
Out[51]:
0 1
0 D1 26.0
1 D2 3.0
2 D3 19.0
3 D4 26.0
4 D5 3.0
5 D6 1.0
6 D7 0.3
7 D8 1.0
对不起,没有说清楚。如果df2在任何一个基因下的值为1,那么对于df1中的每一行,添加(我犯了一个错误,它的ADD不是乘法)与之匹配的行。因此,结果应该类似于D1 26 D2 3 D3 19 D4 26 D5 4 D6 1 D7 0.3 D8 1这很好,但它似乎使用了所有的df2,我需要它仅使用df2的特定行。然后将其用于所有df1数据帧。这就是我设立另一个职位的原因。再次抱歉。谢谢你的帮助。很抱歉没有说清楚。如果df2在任何一个基因下的值为1,那么对于df1中的每一行,添加(我犯了一个错误,它的ADD不是乘法)与之匹配的行。因此,结果应该类似于D1 26 D2 3 D3 19 D4 26 D5 4 D6 1 D7 0.3 D8 1这很好,但它似乎使用了所有的df2,我需要它仅使用df2的特定行。然后将其用于所有df1数据帧。这就是我设立另一个职位的原因。再次抱歉。谢谢你的帮助。很抱歉没有说清楚。如果df2在任何一个基因下的值为1,那么对于df1中的每一行,添加(我犯了一个错误,它的ADD不是乘法)与之匹配的行。因此,结果应该类似于D1 26 D2 3 D3 19 D4 26 D5 4 D6 1 D7 0.3 D8 1这很好,但它似乎使用了所有的df2,我需要它仅使用df2的特定行。然后将其用于所有df1数据帧。这就是我设立另一个职位的原因。再次抱歉。谢谢你的帮助。