Skip to content

Instantly share code, notes, and snippets.

@rafche
Created August 6, 2017 19:25
Show Gist options
  • Select an option

  • Save rafche/a8f84de7ed8ba740e47a70587299e7d8 to your computer and use it in GitHub Desktop.

Select an option

Save rafche/a8f84de7ed8ba740e47a70587299e7d8 to your computer and use it in GitHub Desktop.
feel the NaN Handling of pandas dataframe
import numpy as np
import pandas as pd
nan = np.nan
median_1 = np.median([1,2,3])
median_2 = np.median([0,1,2,3])
median_3 = np.median([0,0,1,2,3])
median_4 = np.median([0,0,0,1,2,3])
mean_1 = np.median([1,2,3])
mean_2 = np.median([0,1,2,3])
mean_3 = np.median([0,0,1,2,3])
mean_4 = np.median([0,0,0,0,1,2,3])
# the dataframe will ignore the nan, see the impact below
df = pd.DataFrame([nan,nan,nan,1,2,3], columns=['A'])
dfa = df.mean()
dfb = df.median()
print(df)
print('\n')
print('\n')
print(dfa)
print(dfb)
print('\n')
print('\n')
# median
print(median_1)
print(median_2)
print(median_3)
print(median_4)
print('\n')
# mean
print(mean_1)
print(mean_2)
print(mean_3)
print(mean_4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment