Skip to content

Instantly share code, notes, and snippets.

@rmwoods
Last active August 14, 2019 18:55
Show Gist options
  • Save rmwoods/934a913fb520ab090a8e1b5b53ef7d25 to your computer and use it in GitHub Desktop.
Save rmwoods/934a913fb520ab090a8e1b5b53ef7d25 to your computer and use it in GitHub Desktop.
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