What are Labels in Pandas Series?
Overview
A one-dimensional labeled array is called the Pandas Series that contains any kind of data (integer, string, float, python objects, etc.). Labels aren't required to be unique, but they ought to be of a hashable type. The object has a variety of ways for using the index in operations and supports both integer and label-based indexing. Statistical algorithms from ndarray have been customized to automatically reject missing data (currently represented as NaN).
Operations between Series(+, -, /, *, **) align values depending on the index values that are connected with them; they do not have to be of the same length. The sorted union of the two indices will be the result index.
Introduction
We begin with a Pandas series example. From a list defined as follows, we produce a straightforward Pandas Series.
Example
Output:
If nothing else is specified, the values are labeled with their index number. The first value has index 0, the second value has index 1 etc. This label can be used to access a specified value.
Example
Output:
Creating Labels
We are able to rename the labels using the index argument. We may use the following example to illustrate any index with string values:
Example
Output:
When you create labels, you may use the label to get to an item.
Example
Output:
Key/Value Objects as Series
Dictionary keys are taken in sorted order to create the index if the dictionary object is being supplied as input and the index is not specified. If the index is accepted, values from the dictionary will be taken out that match a certain label in the index.
Example
Output:
The index labels don't have to be exclusive. You can override the dictionary property by giving the same index dictionary, "Banana".
Example
Output:
Use the index option to specify only the words you wish to be included in the Series, leaving out the rest of the words in the dictionary.
Example
Output:
Accessing Labels Using Label (Index)
1. Accessing a single element using index label We must set values by index label in order to access a series element. In that you may retrieve and set values by index label, a Series is similar to a fixed-size dictionary.
Example
Output:
2. Accessing multiple elements using index label
Example
Output:
3. Access multiple elements by providing a label of index
One of the array-creation methods in NumPy that uses numerical ranges is called arange(). It returns the reference to an instance of ndarray that has values that are uniformly spaced apart.
With the four arguments of arange, you may specify the range of values in an array, the distance between them, and their category.
Example
Output:
4. Accessing multiple elements using an index label in a CSV file
We take the salary prediction dataset from Kaggle, which has parameters like age, workclass, fnlwgt, education, education-num, marital status, occupation, relationship, race, sex, capital gain, capital loss, hours-per-week, native country, and salary.
We import the dataset and print the dataset's subpart.
Example
Output:
More Code Examples
Reference to the index number in order to access the series element. To access an element in a series, type [] in the index operator. An integer must make up the index. We utilize the Slice operation to gain access to numerous items from a series. Series is subjected to a slice operation using the colon (:). Use [:Index] to print elements from beginning to the range, [:-Index] to print elements from range to end, [Index:] to print elements from particular Index to end, [Start Index:End Index] to print elements within range, and [:] to print thought the entire series using slicing operation. Use [::-1] to print the entire Series in reverse order.
1. Accessing the element of the pandas series
Example
Output:
2. Accessing the first 4 elements of the Series
Example
Output:
3. Accessing the last four values using position
Example
Output:
Conclusion
- We learned about pandas and numpy and the main difference between the two.
- Dictionary keys are taken in sorted order to create the index if the dictionary object is being supplied as input and the index is not specified. If the index is accepted, values from the dictionary will be taken out that match a certain label in the index.
- We learned about accessing a single element or multiple using an index label with the help of series[index]
- We can access multiple elements by providing a label of an index like series[[index1, index2, index3]], and accessing multiple elements using an index label in a CSV file. We take a dataset from Kaggle and access the element using numpy.
- We took a look at more code samples, like accessing the first four elements of the Series using the syntax data[:4] and accessing the last four values using position, or we are trying to access the elements with positions using the following syntax:data[-4:].