Pandas DataFrame.add()

编程入门 行业动态 更新时间:2024-10-19 18:33:02
本文介绍了Pandas DataFrame.add()-忽略缺少的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下两个DataFrame:

I have the following two DataFrames:

>>> history above below asn country 12345 US 5 4 MX 6 3 54321 MX 4 5 >>> current above below asn country 12345 MX 1 0 54321 MX 0 1 US 1 0

我像这样在history DataFrame中保持上方"和下方"值的连续计数:

I keep a running count of the "above" and "below" values in the history DataFrame like so:

>>> history = history.add(current, fill_value=0) >>> history above below asn country 12345 MX 7.0 3.0 US 5.0 4.0 54321 MX 4.0 6.0 US 1.0 0.0

只要current DataFrame中没有多余的列,此方法就起作用.但是,当我添加额外的列时:

This works so long as there are no extra columns in the current DataFrame. However when I add an extra column:

>>> current above below cruft asn country 12345 MX 1 0 999 54321 MX 0 1 999 US 1 0 999

我得到以下信息:

>>> history = history.add(current, fill_value=0) >>> history above below cruft asn country 12345 MX 7.0 3.0 999.0 US 5.0 4.0 NaN 54321 MX 4.0 6.0 999.0 US 1.0 0.0 999.0

我希望忽略此多余的列,因为这两个数据帧中都不存在.所需的输出为:

I want this extra column to be ignored, since it's not present in both DataFrames. The desired output is just:

>>> history above below asn country 12345 MX 7.0 3.0 US 5.0 4.0 54321 MX 4.0 6.0 US 1.0 0.0

推荐答案

In [27]: history.add(current, fill_value=0)[history.columns] Out[27]: above below asn country 12345 MX 7.0 3.0 US 5.0 4.0 54321 MX 4.0 6.0 US 1.0 0.0

更多推荐

Pandas DataFrame.add()

本文发布于:2023-11-12 12:11:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Pandas   DataFrame   add

发布评论

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

>www.elefans.com

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