删除 pandas 系列中的空列表 [英] Remove empty lists in pandas series

查看:74
本文介绍了删除 pandas 系列中的空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的长篇小说如下:

series = pd.Series([[(1,2)],[(3,5)],[],[(3,5)]])

In [151]: series
Out[151]:
0    [(1, 2)]
1    [(3, 5)]
2          []
3    [(3, 5)]
dtype: object

我想删除所有带有空白列表的条目.由于某些原因,布尔索引无法正常工作.

I want to remove all entries with an empty list. For some reason, boolean indexing does not work.

以下测试均给出相同的错误:

The following tests both give the same error:

series == [[(1,2)]]
series == [(1,2)]

ValueError: Arrays were different lengths: 4 vs 1

这很奇怪,因为在下面的简单示例中,索引的工作方式与上面相同:

This is very strange, because in the simple example below, indexing works just like above:

In [146]: pd.Series([1,2,3]) == [3]
Out[146]:
0    False
1    False
2     True
dtype: bool

P.S.理想情况下,我也想将系列中的元组也分成两列的DataFrame.

P.S. ideally, I'd like to split the tuples in the series into a DataFrame of two columns also.

推荐答案

您可以使用str.len()检查列表是否为空:

You could check to see if the lists are empty using str.len():

series.str.len() == 0

,然后使用此布尔系列删除包含空列表的行.

and then use this boolean series to remove the rows containing empty lists.

如果每个条目都是一个包含两个元组的列表(否则为空),则可以使用两次str访问器来创建一个两列的DataFrame(一次选择列表的第一个元素,然后访问元组的元素):

If each of your entries is a list containing a two-tuple (or else empty), you could create a two-column DataFrame by using the str accessor twice (once to select the first element of the list, then to access the elements of the tuple):

pd.DataFrame({'a': series.str[0].str[0], 'b': series.str[0].str[1]})

缺少此方法的条目默认为NaN.

Missing entries default to NaN with this method.

这篇关于删除 pandas 系列中的空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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