I am facing a basic problem while using Pandas Python. For example my Dataframe " a " has following columns q,w,e,r. Now I want to take a subset of a.
b=a[[w,e,r,z]]
but it will not create a subset, since z is not there in a, Please help how I can take care of this problem that despite of not finding z in "a" dataframe, I want "b" to be created with rest of them w,e, r.
It appears that using isin
method isn't the most efficient way to do it:
% timeit a[a.columns[a.columns.isin(['w', 'e', 'r', 'z'])]]
out : 1000 loops, best of 3: 528 µs per loop
When you just use a filter:
%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
On the other hand, using isin
automatically reindex your columns like creating a dataframe would:
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments