我想对数据帧中的每一列应用我的自定义ecdf函数,然后根据返回的x,y值绘制ecdf
自定义函数:
def ecdf(df):
n = len(df)
x = np.sort(df)
y = np.arange(1, n+1)/n
return x, y
我的for循环尝试:
^{pr2}$编辑以包含错误:
AxisError Traceback (most recent call last)
<ipython-input-75-d03c4fa0a973> in <module>()
2 #design a for-loop which applies ecdf() on each column in df and plots them separately
3 for col in sj_interpol_data.columns:
----> 4 x_col, y_col = ecdf(col)
5 ax = plt.figure()
6 ax = plt.plot(x_col, y_col, marker='.', linestyle='none')
<ipython-input-32-353fb281e367> in ecdf(df)
4 n = len(df)
5 #define x values - sorted values in array
----> 6 x = np.sort(df)
7 #define y values - maps location of each datapoint WR to their percentiles
8 y = np.arange(1, n+1)/n
C:\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in sort(a, axis, kind, order)
845 else:
846 a = asanyarray(a).copy(order="K")
--> 847 a.sort(axis=axis, kind=kind, order=order)
848 return a
849
AxisError: axis -1 is out of bounds for array of dimension 0
关于如何编写这个函数,以便它可以应用于数据帧中的所有列并在for循环中自动绘图,有什么建议吗?在
我找到了答案。我使用df.sort_值()在ecdf函数中,它使用pandas而不是numpy对值进行排序
所以修改后的函数是:
在应用for循环(如上所示)之后,输出将为dataframe中的每一列生成单独的ecdf图
您向ecdf函数传递了一个列名,但是您希望向其中传递一个数据帧,至少这是您的函数定义所指示的。在
相关问题 更多 >
编程相关推荐