Created
March 10, 2017 01:48
-
-
Save Keiku/57698cae6974d1880862d7673e2a3120 to your computer and use it in GitHub Desktop.
Impute some missing columns with pandas.
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 | |
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