Skip to content

Instantly share code, notes, and snippets.

@Keiku
Created March 10, 2017 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keiku/57698cae6974d1880862d7673e2a3120 to your computer and use it in GitHub Desktop.
Save Keiku/57698cae6974d1880862d7673e2a3120 to your computer and use it in GitHub Desktop.
Impute some missing columns with pandas.
import pandas as pd
df = pd.DataFrame({'A':['A1', 'A2', 'A3'], 'B':[None, 'B2', None]})
df
# Out[51]:
# A B
# 0 A1 None
# 1 A2 B2
# 2 A3 None
# locでimpute
df.loc[(df['B'].isnull()), 'B'] = df['A']
df
# Out[53]:
# A B
# 0 A1 A1
# 1 A2 B2
# 2 A3 A3
# fillnaでimpute
df.A.fillna(df.B, inplace=True)
df
# Out[55]:
# A B
# 0 A1 A1
# 1 A2 B2
# 2 A3 A3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment