Pandas 将数据帧中的列,即df[';常量';]添加到数据帧df的所有其他列。当NAN+;显示float并返回float

Pandas 将数据帧中的列,即df[';常量';]添加到数据帧df的所有其他列。当NAN+;显示float并返回float,pandas,python-2.7,numpy,null,addition,Pandas,Python 2.7,Numpy,Null,Addition,我有一个带有列a、b、c和常量的数据帧df |a |b |c |constant| id | | | | | 000|0 |0.2 |0.5 |0.7 | 111|0 |-0.3|0.1 |0.9 | 222|0 |NAN |0.6 |0.3 | 333|0 |1 |0.8 |0.5 | 444|0 |0.2 |1 |1.1 | 555|0 |0.8 |NAN |-0.3 | 666|0 |-0.5|

我有一个带有列a、b、c和常量的数据帧df

   |a |b   |c   |constant|
id |  |    |    |        |
000|0 |0.2 |0.5 |0.7     |
111|0 |-0.3|0.1 |0.9     |
222|0 |NAN |0.6 |0.3     |
333|0 |1   |0.8 |0.5     |
444|0 |0.2 |1   |1.1     |
555|0 |0.8 |NAN |-0.3    |
666|0 |-0.5|-0.6|NAN     |   
我想将列df['constant']添加到dataframe的所有其他列中,并用sum替换当前列。i、 e.df['a']与df['constant']相同,因为它都是0。新的数据帧应如下所示:

   |a   |b   |c   |constant|
id |    |    |    |        |
000|0.7 |0.9 |1.2 |0.7     |
111|0.9 |0.6 |1   |0.9     |
222|0.3 |0.3 |0.9 |0.3     |
333|0.5 |1.5 |1.3 |0.5     |
444|1.1 |1.3 |2.1 |1.1     |
555|-0.3|0.5 |-0.3|-0.3    |
666|0   |-0.5|-0.6|NAN     |   
注意:如果将NAN添加到浮点,则应返回浮点

我的3次尝试如下所示:

尝试1使用了函数和pd.to\u数值

尝试2我使用了2函数,def添加了(x,y),我希望能够修复类型错误

尝试3我尝试了与尝试2相同的操作,但使用了lambda表达式

df2['constant'] = pd.to_numeric(df2['constant'], errors='coerce')
df2['constant'] = df2['constant'].fillna(0)

dataframe_columns = ['a','b','c']

##attempt number 1
# add merged-constant across df
for c in dataframe_columns:
    df2[c] = pd.to_numeric(df2[c], errors='coerce')
    df2[c] = df2[c].add(df2['constant'])


##attempt number 2

def adds(x,y):
    if isinstance(x, float) and isinstance(y, float)==True:
        return x+y
    elif isinstance(x, float) and isinstance(y, object):
        return x
    elif isinstance(x, object) and isinstance(y, float):
        return y
    else:
        return y

# add merged-constant across df2
for c in dataframe_columns:
    df2[c] = adds(df2[c], dfc['constant'])


##attempt number 3
def adds(x,y):
    if isinstance(x, float) and isinstance(y, float)==True:
        return x+y
    elif isinstance(x, float) and isinstance(y, object):
        return x
    elif isinstance(x, object) and isinstance(y, float):
        return y
    else:
        return y

#lambda test add merged-constant across df2
for c in dataframe_columns:
    return c
df2[dataframe_columns] = df2.apply(lambda x: adds(x[dataframe_columns], x['constant']), axis =  1)


我想要一个新的数据框,a、b和c列都有一个常量列添加到它的总数中,如上图所示。

因为在pd.Series.add中,我们有fill_值=0(不确定为什么数据框+系列不起作用),所以我们只对每个列使用它,然后
concat
back
更新
原始df

df=df.apply(pd.to_numeric,errors='coerce')
df.update(pd.concat([df[x].add(df.constant,fill_value=0) for x in df.columns[:-1]],keys=df.columns[:-1],axis=1))
df
Out[116]: 
     a    b    c  constant
0                         
1  0.7  0.9  1.2       0.7
2  0.9  0.6  1.0       0.9
3  0.3  0.3  0.9       0.3
4  0.5  1.5  1.3       0.5
5  1.1  1.3  2.1       1.1
6 -0.3  0.5 -0.3      -0.3
7  0.0 -0.5 -0.6       NaN

因为在pd.Series.add中,我们有fill_值=0(不确定为什么dataframe+Series不起作用),所以我们只对每个列使用它,然后
concat
back
update
original df

df=df.apply(pd.to_numeric,errors='coerce')
df.update(pd.concat([df[x].add(df.constant,fill_value=0) for x in df.columns[:-1]],keys=df.columns[:-1],axis=1))
df
Out[116]: 
     a    b    c  constant
0                         
1  0.7  0.9  1.2       0.7
2  0.9  0.6  1.0       0.9
3  0.3  0.3  0.9       0.3
4  0.5  1.5  1.3       0.5
5  1.1  1.3  2.1       1.1
6 -0.3  0.5 -0.3      -0.3
7  0.0 -0.5 -0.6       NaN

我使用
fillna
add
join

df[['a', 'b', 'c']].fillna(0).add(df['constant'].fillna(0), axis=0).join(df.constant)


Out[391]:
        a    b    c  constant
id
000   0.7  0.9  1.2       0.7
111   0.9  0.6  1.0       0.9
222   0.3  0.3  0.9       0.3
333   0.5  1.5  1.3       0.5
444   1.1  1.3  2.1       1.1
555  -0.3  0.5 -0.3      -0.3
666   0.0 -0.5 -0.6       NaN

我使用
fillna
add
join

df[['a', 'b', 'c']].fillna(0).add(df['constant'].fillna(0), axis=0).join(df.constant)


Out[391]:
        a    b    c  constant
id
000   0.7  0.9  1.2       0.7
111   0.9  0.6  1.0       0.9
222   0.3  0.3  0.9       0.3
333   0.5  1.5  1.3       0.5
444   1.1  1.3  2.1       1.1
555  -0.3  0.5 -0.3      -0.3
666   0.0 -0.5 -0.6       NaN

谢谢,它可以工作,但是我忘了指定如果有一个实例将NAN添加到NAN,那么NAN将出现在最终的数据帧中。现在所有的N都被转换为零。不幸的是,我需要维护NAN和零,但在添加时充当零。谢谢,它可以工作,但我忘记指定如果有一个实例将NAN添加到NAN,NAN将出现在最终的数据帧中。现在所有的N都被转换为零。不幸的是,我需要维护NaN和Zero,但在添加时充当Zero。