如何基于非空字符串的列对python中的列求和

时间:2017-04-09 17:06:32

标签: python-3.x

df = pd.DataFrame({
'key1':[np.nan,'a','b','b','a'],
'data1':[2,5,8,5,7],
'key2':['ab', 'aa', np.nan, np.nan, 'one'],
'data2':[1,5,9,6,3],
'Sum over columns':[1,10,8,5,10]})

大家好,您能否帮我解决以下问题: 我试图对列进行求和以得到data1和data2的总和。

如果带有字符串(key1)的列不是NaN,而带字符串(key2)的列不是NaN,则将data1和data2相加。我想要的结果显示在sum列中。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

Try using the .apply method of df on axis=1 and numpy's array multiplication function to get your desired output:

import numpy as np
import pandas as pd


df = pd.DataFrame({
'key1':[np.nan,'a','b','b','a'],
'data1':[2,5,8,5,7],
'key2':['ab', 'aa', np.nan, np.nan, 'one'],
'data2':[1,5,9,6,3]})

df['Sum over columns'] = df.apply(lambda x: np.multiply(x[0:2], ~x[2:4].isnull()).sum(), axis=1)

Or:

df['Sum over columns'] = np.multiply(df[['data1','data2']], ~df[['key1','key2']].isnull()).sum(axis=1)

Either one of them should yield:

#    data1  data2 key1 key2  Sum over columns
# 0      2      1  NaN   ab                 1
# 1      5      5    a   aa                10
# 2      8      9    b  NaN                 8
# 3      5      6    b  NaN                 5
# 4      7      3    a  one                10

I hope this helps.