まずフィルタすべてduplicated
行と、その後apply
とgroupby
またはindex
to_series
を変換:
df = df[df.col.duplicated(keep=False)]
a = df.groupby('col').apply(lambda x: list(x.index))
print (a)
col
1 [1, 3, 4]
2 [2, 5]
dtype: object
a = df.index.to_series().groupby(df.col).apply(list)
print (a)
col
1 [1, 3, 4]
2 [2, 5]
dtype: object
そして必要ネストしたリストの場合:
L = df.groupby('col').apply(lambda x: list(x.index)).tolist()
print (L)
[[1, 3, 4], [2, 5]]
のみ最初の使用が必要な場合positioで選択可能な列ですiloc
のn:私はdf.columnsをGROUPBYする必要が列を知らないので、
a = df[df.iloc[:,0].duplicated(keep=False)]
.groupby(df.iloc[:,0]).apply(lambda x: list(x.index))
print (a)
col
1 [1, 3, 4]
2 [2, 5]
dtype: object
はオーケーことはことを除いて、良いことだが、それは大丈夫です。私はグループバイを自分で考えなかったのか分かりません。 – Nico
私は選択のためのソリューションをポジション別に追加します。 – jezrael
これは、複数の列を持つ重複行も検出できますか?私は、** col ** **を表示していないことを意味します。** col1 **、** col2 **、** col3 **などの例では表示されません。 – Nabin