我有两个对象系列,一个之前,一个以后按名称分组。

我从以下代码中生成它们:

beforeseries = dfbefore.groupby('name', dropna=True)['order'].apply(list)
print(beforeseries)
afterseries = dfafter.groupby('name', dropna=True)['order'].apply(list)
print(afterseries)

生成以下输出:

Beforeseries:

Name1 [first, second, third] Name2 [first, second, third] Name_n [first, second, third, fourth]

后期:

Name1 [fourth, fifth] Name2 [fourth, fifth, sixth] Name_n [fifth, sixth]

我想加入它们,以便输出看起来如下”

Name 1 [‘first second third’, ‘fourth fifth’] Name 2 [‘first second third’, ‘fourth fifth sixth’] Name 3 [‘first second third fourth’, ‘fifth sixth’]

分析解答

IIUC,似乎您需要concat两个对象:

out = (
    pd.concat([beforeseries, afterseries], axis=1)
        .groupby(level=0, axis=1).agg(lambda x: x.to_numpy().tolist())
        # .squeeze() # uncomment this chain if you need a Series
)

另一个变体:

# to turn off the FutureWarning in `2.1.0`:
# DataFrame.groupby with axis=1 is deprecated.
# Do `frame.T.groupby(...)` without axis instead.

out = (
    pd.concat([beforeseries, afterseries], axis=1)
        .T.groupby(level=0).agg(list).T
)

或者,不创建中间系列:

keys = ["before", "after"]

out = (
    pd.concat([dfbefore, dfafter], keys=keys, names=["when", None])
        .groupby(by=["name", "when"], sort=False)["order"].agg(" ".join)
        .unstack().agg(list, axis=1)
        # .to_frame("order")
)

输出 :

print(out)

           order
name            
A     [v x, o q]
B         [y, r]
C     [w z, p s]

使用的输入:

dfbefore = pd.DataFrame({"name": list("ACABC"), "order": list("vwxyz")})
beforeseries = dfbefore.groupby("name", dropna=True)["order"].agg(" ".join)

dfafter = pd.DataFrame({"name": list("ACABC"), "order": list("opqrs")})
afterseries  = dfafter.groupby("name", dropna=True)["order"].agg(" ".join)