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.
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework