Skip to content

Instantly share code, notes, and snippets.

@tanveer-sayyed
Last active February 13, 2019 08:14
Show Gist options
  • Save tanveer-sayyed/c04b342597e47bb1c0f5c7ced01af1a5 to your computer and use it in GitHub Desktop.
Save tanveer-sayyed/c04b342597e47bb1c0f5c7ced01af1a5 to your computer and use it in GitHub Desktop.
In [27]:
df.to_csv('Df_with_NaNs', index=False)
In [57]:
new_df = pd.read_csv('Df_with_NaNs', na_values= ['missing', 'not available', 'NA'])
# we should avoid adding the '#$%' value here as a value might be inappropriate
# in one column but not in another.
# eg: 'Male' entered as a value in Age column instead of Gender.
# Setting new index with the same name
new_df['index'] = 'i0,i1,i2,i3,i4,i5'.split(',')
new_df.set_index('index', inplace=True)
#DataFrame is ready !
new_df
Out[57]:
c0 c1 c2 c3
index
i0 1.0 2.0 NaN 3
i1 NaN NaN NaN NaN
i2 4.0 5.0 NaN #$%
i3 6.0 7.0 NaN 8
i4 NaN 9.0 NaN 10
i5 11.0 12.0 NaN NaN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment