I have this kind of dataframe :
data = {'geom1': [17.45, np.nan, 17.445, np.nan], 'geom2': [np.nan,17.45,np.nan,17.455]}
df = pd.DataFrame(data)
I would like to merge the two column of the dataframe
Result expected :
data2 = {'geom': [17.45, 17.45, 17.445, 17.455]}
df_expected = pd.DataFrame(data2)
How should I proceed ?
CodePudding user response:
You can use the lambda function like this
df['geom'] = df.apply(lambda x: x['geom1'] if np.isnan(x['geom2']) else x['geom2'],axis=1)
df_expected = df[['geom']]
CodePudding user response:
if you expect there always to be exactly one column that is non-nullish, you can do:
df.geom1.fillna(df.geom2).to_frame(name='geom')
CodePudding user response:
By using sum
over axis=1
:
df['geom'] = df.sum(axis = 1)
print(df.head())
Output:
geom1 geom2 geom
0 17.450 NaN 17.450
1 NaN 17.450 17.450
2 17.445 NaN 17.445
3 NaN 17.455 17.455
CodePudding user response:
df1 = pd.DataFrame(data['geom1'], columns=['geom1'])
df2 = pd.DataFrame(data['geom2'], columns=['geom2'])
df = DataFrame()
df['geom'] = pd.concat([df1, df2], axis=1).stack().values
or
df = pd.DataFrame()
for k,v in data.items():
df[k] = v
df['geom'] = df.stack().values
df = df.drop(columns=[k for k in data.keys()])
or
df = pd.DataFrame([i for sublist in data.values() for i in sublist], columns=['geom1'])
CodePudding user response:
df.geom1.fillna(0) df.geom2.fillna(0)
or
df.fillna(0).sum(axis = 1)