Merge 2 Columns In Pandas Into One Columns That Have Data In Python
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
Solution 1:
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
Post a Comment for "Merge 2 Columns In Pandas Into One Columns That Have Data In Python"