我有一个大熊猫系列,其元素构成了frozensets:
data = {0: frozenset({'apple', 'banana'}),
1: frozenset({'apple', 'orange'}),
2: frozenset({'banana'}),
3: frozenset({'kumquat', 'orange'}),
4: frozenset({'orange'}),
5: frozenset({'orange', 'pear'}),
6: frozenset({'orange', 'pear'}),
7: frozenset({'apple', 'banana', 'pear'}),
8: frozenset({'banana', 'persimmon'}),
9: frozenset({'apple'}),
10: frozenset({'banana'}),
11: frozenset({'apple'})}
tokens = pd.Series(data); tokens
0 (apple, banana)
1 (orange, apple)
2 (banana)
3 (orange, kumquat)
4 (orange)
5 (orange, pear)
6 (orange, pear)
7 (apple, banana, pear)
8 (persimmon, banana)
9 (apple)
10 (banana)
11 (apple)
Name: Tokens, dtype: object
我想成对应用一个函数。例如,tokens.diff
给出了连续行之间的集合差异:
0 NaN
1 (orange)
2 (banana)
3 (orange, kumquat)
4 ()
5 (pear)
6 ()
7 (apple, banana)
8 (persimmon)
9 (apple)
10 (banana)
11 (apple)
Name: Tokens, dtype: object
我想要同样的事情,但我想要在连续的行上设置联合而不是设置差异。所以,我理想地喜欢:
0 NaN
1 (orange, apple, banana)
2 (banana, orange, apply)
3 (orange, kumquat, banana)
4 (orange, kumquat)
...
如何通过熊猫实现这一目标?我知道我可以使用zip
和list comp来做到这一点,但希望有更好的方法。
答案 0 :(得分:4)
几种方式
选项1] 列表理解
In [3631]: pd.Series([x[0].union(x[1])
for x in zip(tokens, tokens.shift(-1).fillna(''))],
index=tokens.index)
Out[3631]:
0 (orange, banana, apple)
1 (orange, apple, banana)
2 (orange, kumquat, banana)
3 (orange, kumquat)
4 (orange, pear)
5 (orange, pear)
6 (orange, pear, banana, apple)
7 (persimmon, pear, banana, apple)
8 (apple, persimmon, banana)
9 (apple, banana)
10 (banana, apple)
11 (apple)
dtype: object
选项2] map
In [3632]: pd.Series(map(lambda x: x[0].union(x[1]),
zip(tokens, tokens.shift(-1).fillna(''))),
index=tokens.index)
Out[3632]:
0 (orange, banana, apple)
1 (orange, apple, banana)
2 (orange, kumquat, banana)
3 (orange, kumquat)
4 (orange, pear)
5 (orange, pear)
6 (orange, pear, banana, apple)
7 (persimmon, pear, banana, apple)
8 (apple, persimmon, banana)
9 (apple, banana)
10 (banana, apple)
11 (apple)
dtype: object
选项3] 使用concat
和apply
In [3633]: pd.concat([tokens, tokens.shift(-1).fillna('')],
axis=1).apply(lambda x: x[0].union(x[1]), axis=1)
Out[3633]:
0 (orange, banana, apple)
1 (orange, apple, banana)
2 (orange, kumquat, banana)
3 (orange, kumquat)
4 (orange, pear)
5 (orange, pear)
6 (orange, pear, banana, apple)
7 (persimmon, pear, banana, apple)
8 (apple, persimmon, banana)
9 (apple, banana)
10 (banana, apple)
11 (apple)
dtype: object
计时
In [3647]: tokens.shape
Out[3647]: (60000L,)
In [3648]: %timeit pd.Series([x[0].union(x[1]) for x in zip(tokens, tokens.shift(-1).fillna(''))], index=tokens.index)
10 loops, best of 3: 35 ms per loop
In [3649]: %timeit pd.Series(map(lambda x: x[0].union(x[1]), zip(tokens, tokens.shift(-1).fillna(''))), index=tokens.index)
10 loops, best of 3: 40.9 ms per loop
In [3650]: %timeit pd.concat([tokens, tokens.shift(-1).fillna('')], axis=1).apply(lambda x: x[0].union(x[1]), axis=1)
1 loop, best of 3: 2.2 s per loop
与diff
In [3653]: %timeit tokens.diff()
10 loops, best of 3: 10.8 ms per loop