dme*_*meu 2 cell apply dataframe julia
我正在深入研究朱莉娅,因此是我的“新手”问题。
来自 R 和 Python,我习惯于将简单的函数(算术或其他)分别应用于整个pandas.DataFrames 和data.frames。
#both R and Python
df - 1 # returns all values -1, given all values are numeric
df == "someString" # returns a boolean df
Run Code Online (Sandbox Code Playgroud)
有点复杂
#python
df = df.applymap(lambda v: v - 1 if v > 1 else v)
Run Code Online (Sandbox Code Playgroud)
#R
df[] <- lapply(df, function(x) ifelse(x>1,x-1,x))
Run Code Online (Sandbox Code Playgroud)
问题是,我不知道如何在 Julia 中做到这一点,我在网上很难找到模拟解决方案。Stackoverflow 在使用 Google 时有很大帮助。所以在这里。我如何在 Julia 中做到这一点?
谢谢你的帮助!
PS:
到目前为止,我已经提出了以下解决方案,其中我丢失了列名。
DataFrame(colwise(x -> x .-1, df))
# seems like to much code for only subtracting 1 and loosing col names
Run Code Online (Sandbox Code Playgroud)
请将您的 DataFrames.jl 安装更新到版本 0.19.0。
你可以像这样使用广播来做你想做的一切:
julia> df = DataFrame(rand(2,3))
2×3 DataFrame
? Row ? x1 ? x2 ? x3 ?
? ? Float64 ? Float64 ? Float64 ?
????????????????????????????????????????
? 1 ? 0.666871 ? 0.206249 ? 0.729896 ?
? 2 ? 0.547018 ? 0.467758 ? 0.810292 ?
julia> df .+ 1
2×3 DataFrame
? Row ? x1 ? x2 ? x3 ?
? ? Float64 ? Float64 ? Float64 ?
?????????????????????????????????????
? 1 ? 1.66687 ? 1.20625 ? 1.7299 ?
? 2 ? 1.54702 ? 1.46776 ? 1.81029 ?
julia> df .< 0.5
2×3 DataFrame
? Row ? x1 ? x2 ? x3 ?
? ? Bool ? Bool ? Bool ?
????????????????????????????
? 1 ? 0 ? 1 ? 0 ?
? 2 ? 0 ? 1 ? 0 ?
Run Code Online (Sandbox Code Playgroud)
(输出来自 Julia 1.3,但在早期版本中唯一的区别是Bool将在打印输出中显示为true/ false)
这是你想要的吗?