我有两个这样的数据框:
df['one'] = [1,2,3,4,5]
df['two'] = [nan, 15, nan, 22, nan]
我需要某种连接或合并,它会给我这样的数据帧:
df['result'] = [1,15,3,22,5]
任何想法?
答案 0 :(得分:2)
您可以使用np.where
来执行此操作。因此,如果df.two
为NaN
,则使用df.one
的值,否则请使用df.two
。
import pandas as pd
import numpy as np
# your data
# ========================================
df = pd.DataFrame(dict(one=[1,2,3,4,5], two=[np.nan, 15, np.nan, 22, np.nan]))
print(df)
one two
0 1 NaN
1 2 15
2 3 NaN
3 4 22
4 5 NaN
# processing
# ========================================
df['result'] = np.where(df.two.isnull(), df.one, df.two)
one two result
0 1 NaN 1
1 2 15 15
2 3 NaN 3
3 4 22 22
4 5 NaN 5
答案 1 :(得分:2)
您可以使用pandas方法combine_first()
使用另一个值填充DataFrame或Series中的缺失值;在这种情况下,您希望使用df['two']
中的相应值填充df['one']
中的缺失值:
In [342]: df['result']= df['two'].combine_first(df['one'])
In [343]: df
Out[343]:
one two result
0 1 NaN 1
1 2 15 15
2 3 NaN 3
3 4 22 22
4 5 NaN 5