w3resource

Pandas: Extract elements in the given positional indices along an axis of a DataFrame

Pandas Indexing: Exercise-17 with Solution

Write a Pandas program to extract elements in the given positional indices along an axis of a dataframe.

Sample Solution:

Python Code :

import pandas as pd 
import numpy as np
sales_arrays = [['sale1', 'sale1', 'sale3', 'sale3', 'sale2', 'sale2', 'sale4', 'sale4'],
          ['city1', 'city2', 'city1', 'city2', 'city1', 'city2', 'city1', 'city2']]
sales_tuples = list(zip(*sales_arrays))
sales_index = pd.MultiIndex.from_tuples(sales_tuples, names=['sale', 'city'])
print("\nConstruct a Dataframe using the said MultiIndex levels:")
df = pd.DataFrame(np.random.randn(8, 5), index=sales_index)
print(df)
print("\nSelect 1st, 2nd and 3rd row of the said DataFrame:")
positions = [1, 2, 5]
print(df.take([1, 2, 5]))

print("\nTake elements at indices 1 and 2 along the axis 1 (column selection):")
print(df.take([1, 2], axis=1))

print("\nTake elements at indices 4 and 3 using negative integers along the axis 1 (column selection):")
print(df.take([-1, -2], axis=1))

Sample Output:

Construct a Dataframe using the said MultiIndex levels:
                    0         1         2         3         4
sale  city                                                   
sale1 city1  0.913076  0.354532 -0.802093  0.791970 -0.218581
      city2  1.142819 -0.368298  0.215453  0.026137 -0.890050
sale3 city1  0.252533 -0.573312  0.063797  0.785013 -1.089888
      city2 -0.289990  0.122610 -1.694256  0.375177 -1.724325
sale2 city1 -0.369298  0.298590 -1.003860 -0.105213  0.157668
      city2  1.596571  0.010428 -1.182087  0.247076 -0.602570
sale4 city1 -0.060375  1.958592 -1.628266 -0.081583 -0.364392
      city2  0.846199  0.284048  0.799424 -1.486897  0.633890

Select 1st, 2nd and 3rd row of the said DataFrame:
                    0         1         2         3         4
sale  city                                                   
sale1 city2  1.142819 -0.368298  0.215453  0.026137 -0.890050
sale3 city1  0.252533 -0.573312  0.063797  0.785013 -1.089888
sale2 city2  1.596571  0.010428 -1.182087  0.247076 -0.602570

Take elements at indices 1 and 2 along the axis 1 (column selection):
                    1         2
sale  city                     
sale1 city1  0.354532 -0.802093
      city2 -0.368298  0.215453
sale3 city1 -0.573312  0.063797
      city2  0.122610 -1.694256
sale2 city1  0.298590 -1.003860
      city2  0.010428 -1.182087
sale4 city1  1.958592 -1.628266
      city2  0.284048  0.799424

Take elements at indices 4 and 3 using negative integers along the axis 1 (column selection):
                    4         3
sale  city                     
sale1 city1 -0.218581  0.791970
      city2 -0.890050  0.026137
sale3 city1 -1.089888  0.785013
      city2 -1.724325  0.375177
sale2 city1  0.157668 -0.105213
      city2 -0.602570  0.247076
sale4 city1 -0.364392 -0.081583
      city2  0.633890 -1.486897

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to sort a MultiIndex of a DataFrame. Also sort on various levels of index.

Next: Write a Pandas program to get the index of an element of a given Series.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/pandas/index/pandas-indexing-exercise-17.php