简体   繁体   English

在 dataframe 中删除列,分配不能在循环中工作

[英]Dropping column in dataframe with assignment not workng in a loop

I have two dataframes (df_train and df_test) containing a column ('Date') that I want to drop.我有两个数据框(df_train 和 df_test),其中包含我要删除的列(“日期”)。

As far as I understood, I could do it in two ways, ie either by using inplace or by assigning the dataframe to itself, like:据我了解,我可以通过两种方式做到这一点,即使用就地或将 dataframe 分配给自身,例如:

if 'Date' in df_train.columns:
    df_train.drop(['Date'], axis=1, inplace=True)

OR或者

if 'Date' in df_train.columns:
    df_train = df_train.drop(['Date'], axis=1)

Both the methods work on the single dataframe , but the former way should be more memory friendly, since with the assignent a copy of the dataframe is created.这两种方法都适用于单个 dataframe ,但前一种方法应该对 memory 更友好,因为与分配者一起创建了 dataframe 的副本。

The weird thing is, I have to do it for both the dataframes, so I tried to do the same within a loop:奇怪的是,我必须对两个数据框都这样做,所以我尝试在一个循环中做同样的事情:

for data in [df_train, df_test]:
    if 'Date' in data.columns:
        data.drop(['Date'], axis=1, inplace=True)

and

for data in [df_train, df_test]:
    if 'Date' in data.columns:
        data = data.drop(['Date'], axis=1)

and the weird thing is that, in this case, only the first ways (using inplace) works.奇怪的是,在这种情况下,只有第一种方法(使用就地)有效。 If I use the second way, the 'Date' columns aren't dropped.如果我使用第二种方式,则不会删除“日期”列。 Why is that?这是为什么?

It doesn't work because iterating through the list and changing what's in the list doesn't actually change the actual list of dataframes because it only changes the iterators, so you should try:它不起作用,因为遍历列表并更改列表中的内容实际上并不会更改数据帧的实际列表,因为它只会更改迭代器,因此您应该尝试:

lst = []
for data in [df_train, df_test]:
    if 'Date' in data.columns:
        lst.append(data.drop(['Date'], axis=1))
print(lst)

Now lst contains all the dataframes.现在lst包含所有数据帧。

Its better to use a list comprehension :最好使用list comprehension

res = [data.drop(['Date'], axis=1) for data in [df_train, df_test] if 'Date' in data.columns]

Here, you will get a copy of both dataframes after columns are dropped.在这里,您将在删除列后获得两个数据框的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM