I have a merged Pandas dataframe in the following format
index | value_x | value_y |
---|---|---|
0 | nan | 3 |
1 | 3 | nan |
2 | nan | nan |
3 | -1 | 1 |
4 | 6 | nan |
5 | nan | 6 |
6 | -1 | nan |
7 | -1 | 6 |
8 | nan | nan |
Since the original dataframes have the value
field, therefore value_x
and value_y
column is gnerated during the merge process. I would like to merge the two columns so the final column would look like:
index | value_x | value_y | value |
---|---|---|---|
0 | nan | 3 | 3 |
1 | 3 | nan | 3 |
2 | nan | nan | nan |
3 | nan | 1 | 1 |
4 | 6 | nan | 6 |
5 | nan | 6 | 6 |
6 | -1 | nan | -1 |
7 | nan | 6 | 6 |
8 | nan | nan | nan |
In addition, I would like to know if I could avoid the column combining process during the merge process?
Thanks in advance
CodePudding user response:
You can use max
df["value"] = df[["value_x", "value_y"]].max(axis=1)
as this will pick the non-nan value for each row. For this question:
In addition, I would like to know if I could avoid the column combining process during the merge process?
the answer depends on what the two dataframes were before the merge.