あなたは、特定の行に対してデータフレームを比較する(私はdf.iloc[0]
で最初のものを選んだ)、あなたが指定した条件を満足する列を選択するloc
を使用することができます。
df.loc[:, ~(df == df.iloc[0]).all()]
Out:
Col1 Col3 ColY
0 323 324 abc
1 232 342 def
2 364 2343 ghi
タイミング:
@root's suggestion、nunique
は、Seriesを単一の値と比較するよりもかなり高速です。 @MMF suggestedがより効率的なアプローチのように見えるので、あなたが膨大な数のカラム(例えば、何千もの)をカラムに対して反復しない限り。
df = pd.concat([df]*10**5, ignore_index=True)
%timeit df.loc[:, ~(df == df.iloc[0]).all()]
1 loop, best of 3: 377 ms per loop
%timeit df[[col for col in df if not df[col].nunique()==1]]
10 loops, best of 3: 35.6 ms per loop
df = pd.concat([df]*10, axis=1, ignore_index=True)
%timeit df.loc[:, ~(df == df.iloc[0]).all()]
1 loop, best of 3: 3.71 s per loop
%timeit df[[col for col in df if not df[col].nunique()==1]]
1 loop, best of 3: 353 ms per loop
df = pd.concat([df]*3, axis=1, ignore_index=True)
%timeit df.loc[:, ~(df == df.iloc[0]).all()]
1 loop, best of 3: 11.3 s per loop
%timeit df[[col for col in df if not df[col].nunique()==1]]
1 loop, best of 3: 1.06 s per loop
作品を!解決済み。 – xkcd
大きなDFのタイミング比較を追加できますか?私の野生の推測 - あなたの解決策はより速くなるでしょう... – MaxU
@マックス:私のタイミングから、他の解決策は速いです。 – root