从两个不同的数据框中添加两列,其他所有列都相同

时间:2018-04-06 12:06:11

标签: python python-3.x pandas numpy dataframe

如何从两个不同的DataFrames添加两列,其中所有列都相同,除了我要添加的列?

请参阅下面的示例代码:

#IN:
import pandas as pd
import numpy as np
#Observe that 'col2' in both dataframes are equal
df1 = pd.DataFrame({'col1': [3.2, 4.4, 6.5, np.NaN], 'col2': [1.2, 2.2, np.NaN, 5.7]})

     col1    col2
0    3.2     1.2
1    4.4     2.2
2    6.5     nan
3    nan     5.7

df2 = pd.DataFrame({'col1': [1.0, 2.0, 3.0, 4.0], 'col2': [1.2, 2.2, np.NaN, 5.7]})

     col1    col2
0    1.0     1.2
1    2.0     2.2
2    3.0     nan
3    4.0     5.7

# new_df = [INSERT SOME MAGIC FUNCTION HERE]
# I tried: new_df = df1.add(df2, level='col1')    
# but obviously that did not work

#OUT (This is what I want):
print(new_df)

     col1   col2
0    4.2    1.2
1    6.4    2.2
2    9.5    nan
3    4.0    5.7

观察np.NaN + some_float = some_float

2 个答案:

答案 0 :(得分:4)

这是一种方式。添加2个系列时,请记住fillna(0)

res = pd.DataFrame({'col1': df1['col1'].fillna(0) + df2['col1'].fillna(0),
                    'col2': df1['col2']})

print(res)

#    col1  col2
# 0   4.2   1.2
# 1   6.4   2.2
# 2   9.5   NaN
# 3   4.0   5.7

答案 1 :(得分:1)

我认为你可以做到:

new_df = df1.copy()
new_df['col1'] = df1['col1'].fillna(0).add(df2['col1'])

    col1    col2
0   4.2     1.2
1   6.4     2.2
2   9.5     NaN
3   4.0     5.7