Pandas:根据布尔列表/字典替换数据帧列(Pandas: Replace dataframe columns based on Boolean list/dict)

系统教程 行业动态 更新时间:2024-06-14 16:58:30
Pandas:根据布尔列表/字典替换数据帧列(Pandas: Replace dataframe columns based on Boolean list/dict)

我有两个pandas数据框,我想合并在一起,但不是我在我能够找到的例子中看到的方式。 我有一组“旧”数据和一组“新”数据,两个数据框的形状相同,列名相同。 我做了一些分析并确定我需要创建第三个数据集,从“旧”数据中获取一些列,从“新”数据中获取一些列。 举个例子,假设我有这两个数据集:

df_old = pd.DataFrame(np.zeros([5,5]),columns=list('ABCDE')) df_new = pd.DataFrame(np.ones([5,5]),columns=list('ABCDE'))

简单地说:

A B C D E 0 0.0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 0.0 4 0.0 0.0 0.0 0.0 0.0

A B C D E 0 1.0 1.0 1.0 1.0 1.0 1 1.0 1.0 1.0 1.0 1.0 2 1.0 1.0 1.0 1.0 1.0 3 1.0 1.0 1.0 1.0 1.0 4 1.0 1.0 1.0 1.0 1.0

我做了一些分析,发现我想要替换B列和D列。 我可以在这样的循环中做到这一点:

replace = dict(A=False,B=True,C=False,D=True,E=False) df = pd.DataFrame({}) for k,v in sorted(replace.items()): df[k] = df_new[k] if v else df_old[k]

这给了我想要的数据:

A B C D E 0 0.0 1.0 0.0 1.0 0.0 1 0.0 1.0 0.0 1.0 0.0 2 0.0 1.0 0.0 1.0 0.0 3 0.0 1.0 0.0 1.0 0.0 4 0.0 1.0 0.0 1.0 0.0

但是,老实说这看起来有点笨重,而且我想有一种更好的方法来使用熊猫来做到这一点。 另外,我想保留我的列的顺序,这可能不是像这个示例数据集那样按字母顺序排列,所以排序字典可能不是可行的方法,尽管如果我可以从数据集中提取列名,需要。

有没有更好的方法来使用一些Pandas合并功能?

I have two pandas data-frames that I would like to merge together, but not in the way that I've seen in the examples I've been able to find. I have a set of "old" data and a set of "new" data that for two data frames that are equal in shape with the same column names. I do some analysis and determine that I need to create third dataset, taking some of the columns from the "old" data and some from the "new" data. As an example, lets say I have these two datasets:

df_old = pd.DataFrame(np.zeros([5,5]),columns=list('ABCDE')) df_new = pd.DataFrame(np.ones([5,5]),columns=list('ABCDE'))

which are simply:

A B C D E 0 0.0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 0.0 4 0.0 0.0 0.0 0.0 0.0

and

A B C D E 0 1.0 1.0 1.0 1.0 1.0 1 1.0 1.0 1.0 1.0 1.0 2 1.0 1.0 1.0 1.0 1.0 3 1.0 1.0 1.0 1.0 1.0 4 1.0 1.0 1.0 1.0 1.0

I do some analysis and find that I want to replace columns B and D. I can do that in a loop like this:

replace = dict(A=False,B=True,C=False,D=True,E=False) df = pd.DataFrame({}) for k,v in sorted(replace.items()): df[k] = df_new[k] if v else df_old[k]

This gives me the data that I want:

A B C D E 0 0.0 1.0 0.0 1.0 0.0 1 0.0 1.0 0.0 1.0 0.0 2 0.0 1.0 0.0 1.0 0.0 3 0.0 1.0 0.0 1.0 0.0 4 0.0 1.0 0.0 1.0 0.0

but, this honestly seems a bit clunky, and I'd imagine that there is a better way to use pandas to do this. Plus, I'd like to preserve the order of my columns which may not be in alphabetical order like this example dataset, so sorting the dictionary may not be the way to go, although I could probably pull the column names from the data set if need be.

Is there a better way to do this using some of Pandas merge functionality?

最满意答案

一个非常基本的方法就是过滤布尔字典然后直接分配。

to_rep = [k for k in replace if replace[k]] df_old[to_rep] = df_new[to_rep]

如果您想保留旧的DataFrame,可以使用assign()

df_old.assign(**{k: df_new[k] for k in replace if replace[k]})

正如Nickil所提到的,当我们传递一个字典时, assign()显然不会保留参数顺序。 但是为了可预测,它会在DataFrame的末尾按字母顺序插入指定的列。

演示

>>> df_old.assign(**{k: df_new[k] for k in replace if replace[k]}) A B C D E 0 0.0 1.0 0.0 1.0 0.0 1 0.0 1.0 0.0 1.0 0.0 2 0.0 1.0 0.0 1.0 0.0 3 0.0 1.0 0.0 1.0 0.0 4 0.0 1.0 0.0 1.0 0.0

A really rudimentary approach would just be to filter the Boolean dict and then assign directly.

to_rep = [k for k in replace if replace[k]] df_old[to_rep] = df_new[to_rep]

If you wanted to preserve your old DataFrame, you could use assign()

df_old.assign(**{k: df_new[k] for k in replace if replace[k]})

As mentioned by Nickil, assign() evidently doesn't preserve argument order as we're passing a dict. However to be predictable, it inserts the assigned columns in alphabetical order at the end of your DataFrame.

Demo

>>> df_old.assign(**{k: df_new[k] for k in replace if replace[k]}) A B C D E 0 0.0 1.0 0.0 1.0 0.0 1 0.0 1.0 0.0 1.0 0.0 2 0.0 1.0 0.0 1.0 0.0 3 0.0 1.0 0.0 1.0 0.0 4 0.0 1.0 0.0 1.0 0.0

更多推荐

本文发布于:2023-04-15 03:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/d0ad5cf2935efa33b5b3ca1b36ef7a2f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔   字典   数据   列表   Replace

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!