dataframe - 在 Julia 中,通过对列::Float64 进行分箱来对 DataFrame 进行分组

标签 dataframe julia

假设我有一个带有 Float64 列的 DataFrame,我想通过对该列进行分箱来对数据帧进行分组。我听说 cut 函数可能会有所帮助,但它不是在数据帧上定义的。一些工作已经完成( https://gist.github.com/tautologico/3925372 ),但我宁愿使用库函数而不是从互联网复制粘贴代码。指针?

编辑通过 UNIX 时间戳找到按月执行此操作的方法的奖励业力:)

最佳答案

您可以像这样基于Float64列对数据帧进行分箱。这里我的分箱是从 0.0 到 1.0 以 0.1 为增量,根据一列 0.0 到 1.0 之间的 100 个随机数对数据帧进行分箱。

using DataFrames #load DataFrames
df = DataFrame(index = rand(Float64,100)) #Make a DataFrame with some random Float64 numbers
df_array = map(x->df[(df[:index] .>= x[1]) .& (df[:index] .<x[2]),:],zip(0.0:0.1:0.9,0.1:0.1:1.0)) #Map an anonymous function that gets every row between two numbers specified by a tuple called x, and map that anonymous function to an array of tuples generated using the zip function.

这将生成一个包含 10 个数据帧的数组,每个数据帧都有不同的 0.1 大小的 bin。

至于 UNIX 时间戳问题,我不太熟悉这方面的事情,但是在尝试了一下之后,也许这样的事情可以工作:

using Dates

df = DataFrame(unixtime = rand(1E9:1:1.1E9,100)) #Make a dataframe with floats containing pretend unix time stamps
df[:date] = Dates.unix2datetime.(df[:unixtime]) #convert those timestamps to DateTime types
df[:year_month] = map(date->string(Dates.Year.(date))*" "*string(Dates.Month.(date)),df[:date]) #Make a string for every month in your time range
df_array = map(ym->df[df[:year_month] .== ym,:],unique(df[:year_month])) #Bin based on each unique year_month string

关于dataframe - 在 Julia 中,通过对列::Float64 进行分箱来对 DataFrame 进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47391489/

相关文章:

python - 按时间差索引 pandas 数据帧

julia - 如何在 julia 中捕获 linux 信号

julia - 使用 Documenter.jl 添加函数文档字符串

julia - 如何从命令行更新 Julia?

python - 从纬度/经度对的数据框中删除圆括号

r - 根据列条件连接数据框行

python - 我有一个时间序列体积图。想要将另一列数据框强加为虚线

python - 根据现有列中的 bool 值向 pandas 数据框添加列

Julia @code_warntype 揭示了隐藏的临时变量#temp#

julia - 如何在 Julia 中拟合 GLM,同时更改最大迭代次数?