それを使用して表示されます方法はそれを行うための最も効率的な方法ではありません。
% timeit a[a.columns[a.columns.isin(['w', 'e', 'r', 'z'])]]
out : 1000 loops, best of 3: 528 µs per loop
あなただけのフィルターを使用する場合:自動的に希望のデータフレームを作成するようにあなたの列のインデックスを再作成isin
を使用して、一方
%timeit a[[col for col in ['w','e','r','z'] if col in a.columns]]
out: 1000 loops, best of 3: 431 µs per loop
を:
a = pd.DataFrame({'q':[1],'w':[2],'e':[3],'r':[4]})
out: e q r w
0 3 1 4 2
a[a.columns[a.columns.isin(['w', 'e', 'r', 'z'])]]
out : e r w
0 3 4 2
a[[col for col in ['w','e','r','z'] if col in a.columns]]
out: w e r
0 2 3 4
あなたは間違いをした可能性があります。 2番目の 'for'を 'if'に変更することもできます。 – ysearka
@ysearka、ありがとうございます。 – zaxliu