Last active
August 14, 2019 18:55
-
-
Save rmwoods/934a913fb520ab090a8e1b5b53ef7d25 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
df1 = pd.DataFrame([[1,np.nan]], index=[1], columns=["a","b"]) | |
df2 = pd.DataFrame([[np.nan, 2]], ndex=[1], columns=["a","b"]) | |
df3 = df1.append(df2) | |
# a b | |
# 1 1.0 NaN | |
# 1 NaN 2.0 | |
# Combine rows with like indices | |
df4 = df3.groupby(df3.index).first() | |
# | |
# a b | |
# 1 1.0 2.0 | |
# This might be faster for larger DFs? | |
dup = df.loc[df.index.duplicated(keep=False)] | |
dup = dup.groupby(dup.index).first() | |
df5 = df3[~df3.index.duplicated()] | |
df5.loc[dup.index] = dup | |
# a b | |
# 1 1.0 2.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment