Storing and Plotting ECDF values based on the elements in a pandas column

Viewed 550

I have a data frame df

df=

index  value
A      1
B      4
C      8
D      3
E      12
F      7

How to find the Empirical Cumulative Distribution Function (ECDF) of each element in the column df['value'] and store the correspnding value in a separate column df['ecdf']. The formula for ECDF is:

enter image description here

Much more documentation about the ECDF can be found here. I am also interested in plotting a line graph for the CDF obtained.

Expected Output:

df=

index  value  ecdf
A      1      0.1667
B      4      0.5
C      8      0.8333
D      3      0.3333
E      12     1
F      7      0.6667
2 Answers

You can plot CDF by simply using numpy and matplotlib:

import numpy as np
import matplotlib.pyplot as plt

sample = df['value'].values
x = np.sort(sample)
y = np.arange(1,len(x)+1)/float(len(x))
plt.plot(x, y)

Output:

enter image description here

Use Series.rank to calculate numerical data ranks through n with method=max(to assign highest rank in group) and use Series.div to divide this by number of items in value:

df['ecdf'] = df['value'].rank(method='max').div(df['value'].count())

Result:

  index  value      ecdf
0     A      1  0.166667
1     B      4  0.500000
2     C      8  0.833333
3     D      3  0.333333
4     E     12  1.000000
5     F      7  0.666667
Related