我想我会试一试。首先,永远不要在可以避免的地方使用eval。更好的解决方案是使用ast:
import ast
df.A = df.A.apply(ast.literal_eval)
接下来,展平列:
i = df.A.str.len().cumsum() # we'll need this later
df = pd.DataFrame.from_dict(np.concatenate(df.A).tolist())
df.A = df.A.astype(str)
df
A B
0 28 abc
1 29 def
2 30 hij
3 31 hij
4 32 abc
5 28 abc
6 28 abc
7 29 def
8 30 hij
9 28 abc
10 29 klm
11 30 nop
12 28 abc
13 29 xyz
现在,使用i 的间隔执行groupby。
idx = pd.cut(df.index, bins=np.append([0], i), include_lowest=True, right=False)
df = df.groupby(idx, as_index=False).agg(','.join)
df
A B
0 28,29,30 abc,def,hij
1 31,32 hij,abc
2 28 abc
3 28,29,30 abc,def,hij
4 28,29,30 abc,klm,nop
5 28,29 abc,xyz
得到了 Bharath here 的一点帮助。
IntervalIndex (proposed by Wen) 的一个很酷的替代方案是使用 np.put:
i = df.A.str.len().cumsum()
df = pd.DataFrame.from_dict(np.concatenate(df.A).tolist())
df.A = df.A.astype(str)
v = pd.Series(0, index=df.index)
np.put(v, i-1, [1] * len(i))
df = df.groupby(v[::-1].cumsum()).agg(','.join)[::-1].reset_index(drop=True)
df
A B
0 28,29,30 abc,def,hij
1 31,32 hij,abc
2 28 abc
3 28,29,30 abc,def,hij
4 28,29,30 abc,klm,nop
5 28,29 abc,xyz
性能
df = pd.concat([df] * 1000, ignore_index=True)
%%timeit
df.A.apply(pd.Series).stack().\
apply(pd.Series).groupby(level=0).\
agg(lambda x :','.join(x.astype(str)))
1 loop, best of 3: 8.76 s per loop
%%timeit
A = df.A.values.tolist()
B = {
(i, j, k): v
for j, row in enumerate(A)
for i, d in enumerate(row)
for k, v in d.items()
}
pd.Series(B).astype(str).groupby(level=[1, 2]).apply(','.join).unstack()
1 loop, best of 3: 2.08 s per loop
%%timeit
i = df.A.str.len().cumsum()
df2 = pd.DataFrame.from_dict(np.concatenate(df.A).tolist())
df2.A = df2.A.astype(str)
idx = pd.cut(df2.index, bins=np.append([0], i), include_lowest=True, right=False)
df2.groupby(idx, as_index=False).agg(','.join)
1 loop, best of 3: 810 ms per loop
%%timeit
i = df.A.str.len().cumsum()
df2 = pd.DataFrame.from_dict(np.concatenate(df.A).tolist())
df2.A = df2.A.astype(str)
v = pd.Series(0, index=df2.index)
np.put(v, i-1, [1] * len(i))
df2.groupby(v[::-1].cumsum()).agg(','.join)[::-1].reset_index(drop=True)
1 loop, best of 3: 548 ms per loop