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

查看:195
本文介绍了如何突变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,它使用了所谓的pass by share,被价值所传递。因此,当您将DataFrame传递给函数时,将会创建对该函数本地的对DataFrame的新引用。当您重新分配本地的 df 变量并自己引用DataFrame时,它对单独的全局变量及其对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 删除DataFrames中的列。

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

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

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