Python 如果某些值为NaN,如何对数据帧中的2个特定列行求和?

Python 如果某些值为NaN,如何对数据帧中的2个特定列行求和?,python,pandas,dataframe,Python,Pandas,Dataframe,我想创建一个新列,Inches\u z。如果Inches\u x和Inches\u y中都有值,我想将这两个特定列中的值相加,并将其作为同一行中Inches\u z的结果返回。如果存在一个NaN值,我想返回不是NaN值的行值。如果两个值都是NaN,我希望结果值也是NaN。我该怎么做 我有: Name | Inches_x | Feet | Inches_y ------------------------------------ 'Silvers'| 7 | 1 |

我想创建一个新列,
Inches\u z
。如果
Inches\u x
Inches\u y
中都有值,我想将这两个特定列中的值相加,并将其作为同一行中
Inches\u z
的结果返回。如果存在一个
NaN
值,我想返回不是
NaN
值的行值。如果两个值都是
NaN
,我希望结果值也是
NaN
。我该怎么做

我有:

Name    | Inches_x | Feet | Inches_y
------------------------------------
'Silvers'|    7    |   1   |     2
'Jones'  |    7    |   2   |     7
'Jackson'|    4    |  NaN  |    NaN
'Merole' |   NaN   |   2   |     8
'Kanoff' |   NaN   |   5   |    NaN
'Walker' |   NaN   |   8   |     0
'Smith'  |    8    |   0   |     3  
我想:

Name    | Inches_x | Feet | Inches_y | Inches_z
-----------------------------------------------
'Silvers'|    7    |   1   |     2   |    9
'Jones'  |    7    |   2   |     7   |    14
'Jackson'|    4    |  NaN  |    NaN  |    4
'Merole' |   NaN   |   2   |     8   |    8
'Kanoff' |   NaN   |   5   |    NaN  |   NaN
'Walker' |   NaN   |   8   |     0   |    0
'Smith'  |    8    |   0   |     3   |    11

用两列求和并填充

df['z'] = (df['Inches_x'] + df['Inches_y']).fillna(df['Inches_x']).fillna(df['Inches_y'])`
使用显式调用,在这种情况下,当其中一个值(但不是两个)为NaN时,您可以设置一个
fill_值

生成的数据帧符合您的预期:

      Name  Inches_x  Feet  Inches_y  Inches_z
0  Silvers       7.0   1.0       2.0       9.0
1  Jones         7.0   2.0       7.0      14.0
2  Jackson       4.0   NaN       NaN       4.0
3  Merole        NaN   2.0       8.0       8.0
4  Kanoff        NaN   5.0       NaN       NaN
5  Walker        NaN   8.0       0.0       0.0
6  Smith         8.0   0.0       3.0      11.0

您只需将轴=1上的
sum
minu count
一起使用即可:

df["Inches_z"] = df.filter(like="Inches_").sum(1, min_count=1)
print (df)

        Name  Inches_x  Feet  Inches_y  Inches_z
0  'Silvers'       7.0   1.0       2.0       9.0
1    'Jones'       7.0   2.0       7.0      14.0
2  'Jackson'       4.0   NaN       NaN       4.0
3   'Merole'       NaN   2.0       8.0       8.0
4   'Kanoff'       NaN   5.0       NaN       NaN
5   'Walker'       NaN   8.0       0.0       0.0
6    'Smith'       8.0   0.0       3.0      11.0

到底是什么问题?你试过什么,做过什么研究吗?堆栈溢出不是免费的代码编写服务。看:,,谢谢凯南,这正是我需要的!我如何做类似的事情,但不是求和,而是求两列之间的最大值?逻辑是什么?用min或MAXNA填充谢谢!我如何做类似的事情,但不是求和,而是在两列之间找到最大值?@WillWild
df['Inches\u x','Inches\u y']]。最大值(axis=1)
适用于此。谢谢!我如何做类似的事情,但不是求和,而是在两列之间找到最大值?只需调用
max()
min()
,而不是
sum
      Name  Inches_x  Feet  Inches_y  Inches_z
0  Silvers       7.0   1.0       2.0       9.0
1  Jones         7.0   2.0       7.0      14.0
2  Jackson       4.0   NaN       NaN       4.0
3  Merole        NaN   2.0       8.0       8.0
4  Kanoff        NaN   5.0       NaN       NaN
5  Walker        NaN   8.0       0.0       0.0
6  Smith         8.0   0.0       3.0      11.0
df["Inches_z"] = df.filter(like="Inches_").sum(1, min_count=1)
print (df)

        Name  Inches_x  Feet  Inches_y  Inches_z
0  'Silvers'       7.0   1.0       2.0       9.0
1    'Jones'       7.0   2.0       7.0      14.0
2  'Jackson'       4.0   NaN       NaN       4.0
3   'Merole'       NaN   2.0       8.0       8.0
4   'Kanoff'       NaN   5.0       NaN       NaN
5   'Walker'       NaN   8.0       0.0       0.0
6    'Smith'       8.0   0.0       3.0      11.0