如何改变 DataFrame? [英] How to Mutate a DataFrame?

查看:24
本文介绍了如何改变 DataFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的数据框中删除一些列,并且不希望返回修改后的数据框并将其重新分配给旧数据框.相反,我希望该函数仅修改数据框.这是我尝试过的,但它似乎并没有做我除了.我的印象是参数作为引用而不是值传递?

I am trying to remove some columns from my data frame and would prefer not to return the modified data frame and reassign it to the old. Instead, I would like the function to just modify the data frame. This is what I tried but it does not seem to be doing what I except. I was under the impression arguments as passed as reference and not by value?

function remove_cols! (df::DataFrame, cols)   
  df = df[setdiff(names(df), cols)];
end

df = DataFrame(x = [1:10], y = [11:20]);
remove_cols!(df, [:y]); # this does not modify the original data frame

当然,下面的方法有效,但如果 remove_cols! 只是将 df 更改到位,我会更喜欢

Of course the below works but I would prefer if remove_cols! just changed the df in place

df = remove_cols!(df, [:y]);

如何更改函数内部的 df?

How can I change the df in place inside my function?

谢谢!

推荐答案

据我了解,Julia 它使用所谓的共享传递,这意味着引用是按值传递的.因此,当您将 DataFrame 传递给函数时,会创建对该函数本地的 DataFrame 的新引用.当您使用自己对 DataFrame 的引用重新分配本地 df 变量时,它对单独的全局变量及其对 DataFrame 的单独引用没有影响.

As I understand Julia it uses what is called pass by sharing, meaning that the reference is passed by value. So when you pass the DataFrame to the function a new reference to the DataFrame is created which is local to the function. When you reassign the local df variable with its own reference to the DataFrame it has no effect on the separate global variable and its separate reference to the DataFrame.

DataFrames.jl中有一个函数删除 DataFrame 中的列.

There is a function in DataFrames.jl for deleting columns in DataFrames.

这篇关于如何改变 DataFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆