I would like to merge two columns into one but I am not sure how to do this efficiently. My df looks like this:
col1 col2
0.4 -0.9
0.2 -0.5
-0.1 0.2
-0.2 0.4
0.8 -0.6
So if one column is positive, the other one is always negative. But I would like to have all negative numbers from column 1 replaced by all positive numbers from column 2. So it would look like this:
col1
0.4
0.2
0.2
0.4
0.8
If you know an efficient solution to this I would really appreciate it!!
CodePudding user response:
Find the rows where col1 is less than 0 and replace with col2:
df.loc[df['col1'] < 0, 'col1'] = df['col2']
result:
col1 col2
0 0.4 -0.9
1 0.2 -0.5
2 0.2 0.2
3 0.4 0.4
4 0.8 -0.6
CodePudding user response:
You could use a mask for all negative values and fill the missing values with values of col2
.
m = df['col1'] < 0
df['col1'] = df['col1'].mask(m).fillna(df['col2'])
print(df)
col1 col2
0 0.4 -0.9
1 0.2 -0.5
2 0.2 0.2
3 0.4 0.4
4 0.8 -0.6
CodePudding user response:
using df.where
& .drop
df_new = df.where(df['col1'].ge(0),df['col2'],axis=0).drop('col2',axis=1)
print(df_new)
col1
0 0.4
1 0.2
2 0.2
3 0.4
4 0.8
CodePudding user response:
You can apply your selection function on the Dataframe and Drop the second column afterwards.
df["col1"] = df.apply(lambda row: max(row.col1, row.col2), axis=1)
df = df.drop("col2", axis=1)