Python | Pandas TimedeltaIndex.get_level_values
Last Updated :
28 Dec, 2018
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas TimedeltaIndex.get_level_values()
function return an Index of values for requested level, equal to the length of the index.
Syntax : TimedeltaIndex.get_level_values(level)
Parameters :
level : level is either the integer position of the level in the MultiIndex, or the name of the level.
Return : self, as there is only one level in the Index.
Example #1: Use TimedeltaIndex.get_level_values()
function to find all the values present in the 0th level of the given TimedeltaIndex object.
import pandas as pd
tidx = pd.TimedeltaIndex(data = [ '3 days 06:05:01.000030' , '1 days 06:05:01.000030' ,
'3 days 06:05:01.000030' , '1 days 02:00:00' ,
'21 days 06:15:01.000030' ])
print (tidx)
|
Output :
Now we will use the TimedeltaIndex.get_level_values()
function to find all the values in 0th level
Output :
As we can see in the output, the TimedeltaIndex.get_level_values()
function has returned all the values present in the 0th level of tidx object. This object has only one level.
Example #2: Use MultiIndex.get_level_values()
function to find all the values present in the 1st level of the given MultiIndex object.
import pandas as pd
midx = pd.MultiIndex.from_arrays(([ 'AB' , 'BC' , 'CD' , 'DE' ],
[ 'EF' , 'FG' , 'GH' , 'HI' ]))
print (midx)
|
Output :
Now we will use the MultiIndex.get_level_values()
function to find all values in the 1st level
Output :
As we can see in the output, the MultiIndex.get_level_values()
function has returned all the values present in the 1st level of midx object.
Share your thoughts in the comments
Please Login to comment...