Slice a Pandas dataframe by an array of indices and column names

Artturi Björk

I'm looking to replicate the behavior of a numpy array with a pandas dataframe. I want to pass an array of indices and column names and get a list of objects that are found in the corresponding index and column name.

import pandas as pd
import numpy as np

In numpy:

array=np.array(range(9)).reshape([3,3])
print array
print array[[0,1],[0,1]]

[[0 1 2]
 [3 4 5]
 [6 7 8]]

[0 4]

In pandas:

prng = pd.period_range('1/1/2011', '1/1/2013', freq='A')
df=pd.DataFrame(array,index=prng)
print df

      0  1  2
2011  0  1  2
2012  3  4  5
2013  6  7  8

df[[2011,2012],[0,1]]

Expected output:

[0 4]

How should I slice this dataframe to get it to return the same as numpy?

Jeff

Pandas doesn't support this directly; it could, but the issue is how to specify that you want coordinates rather than different axes, e.g. df.iloc[[0,1],[0,1]] means give me the 0 and 1st rows and the 0 and 1st column.

That said, you can do this:

You updated the question and say you want to start with the index values

In [19]: row_indexer = df.index.get_indexer([Period('2011'),Period('2012')])

In [20]: col_indexer = df.columns.get_indexer([0,1])

In [21]: z = np.zeros(df.shape,dtype=bool)

In [22]: z[row_indexer,col_indexer] = True

In [23]: df.where(z)
Out[23]: 
       0   1   2
2011   0 NaN NaN
2012 NaN   4 NaN
2013 NaN NaN NaN

This seems easier though (these are the locations)

In [63]: df.values[[0,1],[0,1]]
Out[63]: array([0, 4])

Or this; as the Period index will be sliced correctly from the strings (don't use integers here)

In [26]: df.loc['2011',0]
Out[26]: 0

In [27]: df.loc['2012',1]
Out[27]: 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

Slice a Pandas dataframe by an array of indices and column names

From Dev

Is it possible to select pandas dataframe with row indices and column names?

From Dev

Set Column Names to Indices in a Dataframe

From Dev

How to slice a numpy array by a list of column indices

From Dev

How to slice a numpy array by a list of column indices

From Dev

Slice pandas DataFrame where column's value exists in another array

From Dev

slicing pandas dataframe with an array of indices

From Dev

How to convert a pandas dataframe into a numpy array with the column names

From Dev

How do I turn an array of column names into a pandas Dataframe?

From Dev

Python Pandas Setting Dataframe index and Column names from an array

From Dev

Compare column names of Pandas Dataframe

From Dev

Slice pandas dataframe based on datetime column

From Dev

Slice pandas dataframe json column into columns

From Dev

Slice pandas dataframe based on datetime column

From Dev

Convert numpy array with indices to a pandas dataframe

From Dev

How to slice a dataframe with specific column names + range of columns?

From Dev

how to create a pandas DataFrame by combining a list of column_names and a numpy array, and then adding more column(s)?

From Dev

Merging pandas column from dataframe to another dataframe based on their indices

From Java

Select a pandas DataFrame column by indices given in another column

From Dev

Pandas dataframe: slicing column values using second column for slice index

From Dev

slice pandas dataframe by column showing all except the column provided

From Java

Pandas create empty DataFrame with only column names

From Dev

Replace values in pandas dataframe based on column names

From Dev

Pandas make a new dataframe with the old column names

From Dev

Set column names when stacking pandas DataFrame

From Dev

Build list from column names Pandas DataFrame

From Dev

Sorting data in a pandas DataFrame by names in column

From Dev

how to get numeric column names in pandas dataframe

From Dev

Pandas create empty DataFrame with only column names

Related Related

  1. 1

    Slice a Pandas dataframe by an array of indices and column names

  2. 2

    Is it possible to select pandas dataframe with row indices and column names?

  3. 3

    Set Column Names to Indices in a Dataframe

  4. 4

    How to slice a numpy array by a list of column indices

  5. 5

    How to slice a numpy array by a list of column indices

  6. 6

    Slice pandas DataFrame where column's value exists in another array

  7. 7

    slicing pandas dataframe with an array of indices

  8. 8

    How to convert a pandas dataframe into a numpy array with the column names

  9. 9

    How do I turn an array of column names into a pandas Dataframe?

  10. 10

    Python Pandas Setting Dataframe index and Column names from an array

  11. 11

    Compare column names of Pandas Dataframe

  12. 12

    Slice pandas dataframe based on datetime column

  13. 13

    Slice pandas dataframe json column into columns

  14. 14

    Slice pandas dataframe based on datetime column

  15. 15

    Convert numpy array with indices to a pandas dataframe

  16. 16

    How to slice a dataframe with specific column names + range of columns?

  17. 17

    how to create a pandas DataFrame by combining a list of column_names and a numpy array, and then adding more column(s)?

  18. 18

    Merging pandas column from dataframe to another dataframe based on their indices

  19. 19

    Select a pandas DataFrame column by indices given in another column

  20. 20

    Pandas dataframe: slicing column values using second column for slice index

  21. 21

    slice pandas dataframe by column showing all except the column provided

  22. 22

    Pandas create empty DataFrame with only column names

  23. 23

    Replace values in pandas dataframe based on column names

  24. 24

    Pandas make a new dataframe with the old column names

  25. 25

    Set column names when stacking pandas DataFrame

  26. 26

    Build list from column names Pandas DataFrame

  27. 27

    Sorting data in a pandas DataFrame by names in column

  28. 28

    how to get numeric column names in pandas dataframe

  29. 29

    Pandas create empty DataFrame with only column names

HotTag

Archive