如何计算数据帧中的总元素数,包括子集,并将结果放在新列中?
import pandas as pd
x = pd.Series([[1, (2,5,6)], [2, (3,4)], [3, 4], [(5,6), (7,8,9)]],
index=range(1, len(x)+1))
df = pd.DataFrame({'A': x})
我尝试使用以下代码,但它在每行中给出 2 个:
df['Length'] = df['A'].apply(len)
print(df)
A Length
1 [1, (2, 5, 6)] 2
2 [2, (3, 4)] 2
3 [3, 4] 2
4 [(5, 6), (7, 8, 9)] 2
但是,我想得到的是:
A Length
1 [1, (2, 5, 6)] 4
2 [2, (3, 4)] 3
3 [3, 4] 2
4 [(5, 6), (7, 8, 9)] 5
谢谢
给定:
import pandas as pd
x = pd.Series([[1, (2,5,6)], [2, (3,4)], [3, 4], [(5,6), (7,8,9)]])
df = pd.DataFrame({'A': x})
您可以编写一个递归生成器,该生成器将为每个不可迭代的嵌套元素生成1
。大致如下:
import collections
def glen(LoS):
def iselement(e):
return not(isinstance(e, collections.Iterable) and not isinstance(e, str))
for el in LoS:
if iselement(el):
yield 1
else:
for sub in glen(el): yield sub
df['Length'] = df['A'].apply(lambda e: sum(glen(e)))
屈服:
>>> df
A Length
0 [1, (2, 5, 6)] 4
1 [2, (3, 4)] 3
2 [3, 4] 2
3 [(5, 6), (7, 8, 9)] 5
这将在Python 2或3中工作。在 Python 3.3 或更高版本中,您可以使用 yield from
来替换循环:
def glen(LoS):
def iselement(e):
return not(isinstance(e, collections.Iterable) and not isinstance(e, str))
for el in LoS:
if iselement(el):
yield 1
else:
yield from glen(el)
使用itertools
df['Length'] = df['A'].apply(lambda x: len(list(itertools.chain(*x))))
你可以尝试使用这个函数,它是递归的,但它有效:
def recursive_len(item):
try:
iter(item)
return sum(recursive_len(subitem) for subitem in item)
except TypeError:
return 1
然后只需以这种方式调用 apply 函数:
df['Length'] = df['A'].apply(recursive_len)