简体   繁体   English

Pandas df 重新排序列似乎在循环中工作,但没有。 我到底错过了什么?

[英]Pandas df re-ordering columns seems to work within a loop, but doesn't. What the heck am I missing?

So I'm completely perplexed as to why this is happening:所以我完全不明白为什么会发生这种情况:

I have 8 different Pandas dataframes, with same columns.我有 8 个不同的 Pandas 数据框,具有相同的列。 I want to rearrange the columns equally on all of them.我想在所有列上均等地重新排列列。 So I created a list and tried this:所以我创建了一个列表并尝试了这个:

original_cols = [1, 48, 49, 50, 51, 52]
new_cols = [48, 49, 50, 51, 52, 1]

list_of_dfs = [df1, df2, df3...., df8]

for df in list_of_dfs:
    df = df[new_cols]

When I look at any of the dataframes, I still get the old column order, why?当我查看任何数据框时,我仍然得到旧的列顺序,为什么? I inserted a print statement as below, and the loop does what I want:我插入了如下打印语句,循环执行我想要的操作:

for df in list_of_dfs:
    print (df.columns.tolist())
    df = df[new_cols]
    print (df.columns.tolist())

Output (for df1):
[1, 48, 49, 50, 51, 52]
[48, 49, 50, 51, 52, 1]

I can just write out all manually, but thought a simple loop would be better but can't get it to work.我可以手动写出所有内容,但认为一个简单的循环会更好,但无法使其工作。 I must be missing some fundamental understanding of loops or something.我一定缺少对循环或其他东西的一些基本理解。 Any help is greatly appreciated.任何帮助是极大的赞赏。

Current solution:当前解决方案:

df1 = df1[new_cols]
df2 = df2[new_cols]
.
.
```

When you assign df = df[new_cols] it is not updating the DataFrame in the list.当您分配df = df[new_cols]它不会更新列表中的 DataFrame。 Try this:尝试这个:

size_ = len(list_of_dfs)
for idx in range(size_):
    list_of_dfs[idx] = list_of_dfs[idx][new_cols]

Now idx will represent an index location in list_of_dfs and you can just update the DataFrame columns at each index.现在 idx 将代表list_of_dfs的索引位置,您可以只更新每个索引处的 DataFrame 列。

You are referring to a copy of the DataFrame object.您指的是DataFrame对象的副本。 If you need to swap variable names in the global scope (not recommended), you may use globals to refer to the object itself.如果您需要在全局范围内交换变量名(不推荐),您可以使用globals来引用对象本身。

import re
for df in [name for name in globals() if re.findall('df\d+', name)]:
    globals()[df] = globals()[df][new_cols]

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

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