pandas - 将用熊猫切割的垃圾箱传递给一个函数
问题描述
我有一个数据框,如下所示:
def binning(col, cut_points, labels = None):
'''
From https://www.analyticsvidhya.com/blog/2016/01/12-pandas-techniques-python-data-manipulation/
'''
# Define min and max values:
minval = col.min()
maxval = col.max()
# Create list by adding min and max to cut_points
break_points = [minval] + cut_points + [maxval]
# If no labels provided, use default labels 0 ... (n-1)
if not labels:
labels = range(len(cut_points)+1)
# Binning using cut function of pandas
colBin = pd.cut(col,bins=break_points,labels=labels,include_lowest=True, duplicates = 'drop')
return colBin
cut_points = [0.5,3.5,4.5]
labels = ["z<0.5","0.5<=z<3.5","3.5<z<=4.5","z>4.5"]
sources["z_bin"] = binning(sources["z"], cut_points, labels)
print(pd.value_counts(sources["z_bin"], sort=False))
我想将每个 bin 传递给我编写的函数,以绘制散点图。我知道它pandas
有绘图函数和包装器matplotlib
,但如果可能的话,我想使用我的自定义函数,以保持格式与我的其他图表一致。我的自定义函数调用如下:
plotSelected(x, y, name_for_y_series, ...a couple of other arguments)
那么有什么方法可以将 y 系列与分箱的 x 值相对应吗?就像是
plotSelected(x_binned, y, name_for_y_series, ...a couple of other arguments)
我不知道如何pandas
组织它的垃圾箱。它们是列表、元组还是其他东西?
解决方案
推荐阅读
- jquery - 在 2 级菜单时打开顶层 - Woocommerce
- apache - 无法使用 WAMP64 从我自己的 PC 托管网站
- php - 从 3 个表中获取最后的记录
- string - 从 VBA 集合中快速删除重复的字符串
- javascript - 由于 CORB,无法获取 OG 数据
- oracle - 插入和删除时Oracle触发器更新字段(字段+1和字段-1)
- php - 测试记录是否存在并选择sql查询
- javascript - 未获取返回有效 JSON 的 Localhost API
- android - 隐藏特定时间的状态栏和标题栏
- php - 允许用户在 TWIG 中添加任何代码是一个安全漏洞吗?