def via_cc(df_in):
    df = df_in.copy()
dfr = df[['y', 'z']].rank(method='dense')
dfr = dfr.fillna(dfr.max().fillna(0) + dfr.isnull().cumsum(axis=0))
dfr['z'] += dfr['y'].max()
size = int(dfr.max().max()) + 1
m = scipy.sparse.coo_matrix(([1] * len(dfr), (dfr.y, dfr.z)), (size, size))
_, cc = scipy.sparse.csgraph.connected_components(m)
group = pd.Series(cc[dfr['y'].astype(int).values], index=dfr.index)
df['w'] = df['x'].groupby(group).transform(min)
return df
Возьмите объединение двух столбцов, Python + Pandas