0
0
Data Analysis Pythondata~5 mins

Series indexing and selection in Data Analysis Python

Choose your learning style9 modes available
Introduction

We use series indexing and selection to pick specific data points from a list of values. It helps us focus on the data we want to analyze.

You want to get a single value from a list of numbers, like a temperature on a certain day.
You need to select multiple values from a list, such as sales figures for specific products.
You want to filter data based on conditions, like finding all scores above 80.
You want to access data by position or by label, depending on how your data is organized.
Syntax
Data Analysis Python
series[label]
series[start_label:end_label]
series[position]
series[start_pos:end_pos]
series[condition]

You can select data by labels (names) or by positions (numbers).

Using conditions lets you filter data easily.

Examples
Selects the value with label 'b', which is 20.
Data Analysis Python
import pandas as pd

s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(s['b'])
Selects the first value by position, which is 10.
Data Analysis Python
print(s[0])
Selects values from label 'a' to 'b' inclusive.
Data Analysis Python
print(s['a':'b'])
Selects values greater than 15, which are 20 and 30.
Data Analysis Python
print(s[s > 15])
Sample Program

This program shows how to pick single and multiple scores by label and how to filter scores above 80.

Data Analysis Python
import pandas as pd

# Create a series with labels
scores = pd.Series([88, 92, 79, 93], index=['Math', 'English', 'History', 'Science'])

# Select score for English
english_score = scores['English']

# Select scores for Math and Science
math_science_scores = scores[['Math', 'Science']]

# Select scores greater than 80
high_scores = scores[scores > 80]

print('English score:', english_score)
print('Math and Science scores:\n', math_science_scores)
print('Scores above 80:\n', high_scores)
OutputSuccess
Important Notes

When selecting by label with slices, the end label is included.

When selecting by position with slices, the end position is excluded.

Using double brackets like series[['a', 'b']] selects multiple labels.

Summary

Series indexing lets you pick data by label or position.

You can select single values, multiple values, or filter with conditions.

Label slices include the end label; position slices exclude the end position.