Indexing and selecting only found columns in Pandas python

Manu Sharma

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.

ysearka

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Indexing and selecting only found columns in Pandas python

From Dev

Pandas DataFrame indexing, Selecting rows with specific columns that are NaN values

From Dev

Selecting Pandas Columns by dtype

From Dev

Indexing/selecting by year in Pandas when MultiIndex is in use

From Dev

python pandas selecting columns from a dataframe via a list of column names

From Dev

python pandas selecting columns from a dataframe via a list of column names

From Dev

Indexing and Data Columns in Pandas/PyTables

From Dev

Python Pandas drop columns not found in both dataframes

From Dev

Python Pandas indexing

From Dev

Python Pandas Indexing Reform

From Dev

Indexing a pandas dataframe in Python

From Java

Selecting/excluding sets of columns in pandas

From Java

Selecting multiple columns in a pandas dataframe

From Dev

Selecting sublevels of Multiindex columns in pandas

From Dev

Selecting columns from Pandas DataFrame

From Dev

Selecting columns with condition on Pandas DataFrame

From Dev

Pandas: selecting multiple columns programmatically

From Dev

Selecting columns from Pandas DataFrame

From Dev

Pandas: selecting multiple columns programmatically

From Java

Python Pandas merge only certain columns

From Dev

Selecting columns equal to a substring in python

From Dev

Pandas apply function to groups of columns and indexing

From Dev

Comparing mutiple columns and selecting only one

From Dev

Indexing by multiple fields with pandas in python

From Dev

Pandas selecting discontinuous columns from a dataframe

From Dev

Pandas selecting columns - best habit and performance

From Dev

Selecting columns of a pandas dataframe based on criteria

From Dev

Selecting columns of a pandas dataframe based on criteria

From Dev

Python Pandas - Selecting specific rows based on the max and min of two columns with the same group id

Related Related

  1. 1

    Indexing and selecting only found columns in Pandas python

  2. 2

    Pandas DataFrame indexing, Selecting rows with specific columns that are NaN values

  3. 3

    Selecting Pandas Columns by dtype

  4. 4

    Indexing/selecting by year in Pandas when MultiIndex is in use

  5. 5

    python pandas selecting columns from a dataframe via a list of column names

  6. 6

    python pandas selecting columns from a dataframe via a list of column names

  7. 7

    Indexing and Data Columns in Pandas/PyTables

  8. 8

    Python Pandas drop columns not found in both dataframes

  9. 9

    Python Pandas indexing

  10. 10

    Python Pandas Indexing Reform

  11. 11

    Indexing a pandas dataframe in Python

  12. 12

    Selecting/excluding sets of columns in pandas

  13. 13

    Selecting multiple columns in a pandas dataframe

  14. 14

    Selecting sublevels of Multiindex columns in pandas

  15. 15

    Selecting columns from Pandas DataFrame

  16. 16

    Selecting columns with condition on Pandas DataFrame

  17. 17

    Pandas: selecting multiple columns programmatically

  18. 18

    Selecting columns from Pandas DataFrame

  19. 19

    Pandas: selecting multiple columns programmatically

  20. 20

    Python Pandas merge only certain columns

  21. 21

    Selecting columns equal to a substring in python

  22. 22

    Pandas apply function to groups of columns and indexing

  23. 23

    Comparing mutiple columns and selecting only one

  24. 24

    Indexing by multiple fields with pandas in python

  25. 25

    Pandas selecting discontinuous columns from a dataframe

  26. 26

    Pandas selecting columns - best habit and performance

  27. 27

    Selecting columns of a pandas dataframe based on criteria

  28. 28

    Selecting columns of a pandas dataframe based on criteria

  29. 29

    Python Pandas - Selecting specific rows based on the max and min of two columns with the same group id

HotTag

Archive