0

I"m not sure How to word this but I'm trying to combine 2 columns say from datafram df :

x1  x2
NaN  3
2    2
4    2
NaN  5
8    NaN

so I would like to get result of

X
3
2
4
5
8

I tried to use pd.merge but this won't work. pretty new to python can you guide me which function do I need in this situation ?

3
  • So if x1 and x2 are both non-NaN, you want x1? Commented Dec 5, 2013 at 17:36
  • is your desired merge operation (logically) "max"? Commented Dec 5, 2013 at 17:38
  • yes, I would like capture the first one which is x1 Commented Dec 5, 2013 at 17:47

1 Answer 1

2

It's not clear from your question whether you want x1 to dominate over x2 (my first guess) or whether you want the maximum of both columns (@RobStarling's guess). We can do either. To see the difference, we'll change your frame slightly:

>>> df = pd.DataFrame({'x1': {0: np.nan, 1: 2.0, 2: 4.0, 3: 1, 4: 8.0}, 'x2': {0: 3.0, 1: 2.0, 2: 2.0, 3: 5.0, 4: np.nan}})
>>> df
   x1  x2
0 NaN   3
1   2   2
2   4   2
3   1   5
4   8 NaN

[5 rows x 2 columns]

If you want x1 to win, we can use where-- we want to use x1 wherever it's not null, and x2 otherwise:

>>> df["x1"].where(~pd.isnull(df["x1"]), df["x2"])
0    3
1    2
2    4
3    1
4    8
Name: x1, dtype: float64

If you want the maximum of either:

>>> df[["x1", "x2"]].max(axis=1)
0    3
1    2
2    4
3    5
4    8
dtype: float64
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.