pandas :添加包含列表的多索引系列/数据框 [英] Pandas: adding multiindex Series/Dataframes containing lists

查看:144
本文介绍了 pandas :添加包含列表的多索引系列/数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加/合并两个包含列表作为元素的多索引Series / DataFrame(在我的情况下为端口序列或时间戳序列)。特别是,如何处理仅出现在一个Series / DataFrame中的索引?不幸的是, .add()方法只允许对 fill_value 参数使用浮点数,而不允许使用空列表。

How do I add / merge two multiindex Series/DataFrames which contain lists as elements (a port-sequence or timestamp-sequence in my case). Especially, how to deal with indices, which appear only in one Series/DataFrame? Unfortunately, the .add()-method allows only floats for the fill_value argument, not empty lists.

我的数据:

print series1
print series2

IP               sessionID
195.12*.21*.11*  49                    [5900]
                 50         [5900, 5900, 5900, 5900, ...

IP               sessionID
85.15*.24*.12*   63                    [3389]
91.20*.4*.14*    68           [445, 445, 139]
113.9*.4*.16*    75                 [23, 210]
195.12*.21*.11*  49                    [5905]

预期结果:

IP               sessionID
195.12*.21*.11*  49              [5900, 5905]
                 50         [5900, 5900, 5900, 5900, ...
85.15*.24*.12*   63                    [3389]
91.20*.4*.14*    68           [445, 445, 139]
113.9*.4*.16*    75                 [23, 210]

奇怪的是足够了, series1.add(series1) series2.add(series2)确实可以工作并附加lis ts符合预期,但是 series1.add(series2)会产生运行时错误。 series1.combine_first(series2)可以工作,但是它不会合并列表-它只需要一个即可。是吗?

Oddly enough, series1.add(series1) or series2.add(series2) does work and appends the lists as expected, however series1.add(series2) produces runtime errors. series1.combine_first(series2) works, however it does not merge the lists - it simply takes one. Any ideas?

是的,我知道列表元素是不好的样式,但这就是我的数据现在的样子。抱歉为了简短起见,我只是发布了系列示例,让我知道您是否还需要DataFrame示例。

推荐答案

万一有其他可怜的幽灵需要此信息...
似乎是一种肮脏的解决方法,但它可以起作用:

In case there is any other poor ghost out there which needs this info... It seems like a dirty work-around, but it works:

# add() works for mutual indices, so find intersection and call it
# fortunately, it appends list2 to list1!
intersection = series1.index.intersection(series2.index)
inter1 = series1[series1.index.isin(intersection)]
inter2 = series2[series2.index.isin(intersection)]
interAppend = inter1.add(inter2)

# combine_first() unions indices and keeps the values of the caller,
# so it will keep the appended lists on mutual indices,
# while it adds new indices and corresponding values
exclusiveAdd = interAppend.combine_first(series1).combine_first(series2)

这篇关于 pandas :添加包含列表的多索引系列/数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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