Skip to main content

Command Palette

Search for a command to run...

Series Methods & Properties

Published
4 min read
Series Methods & Properties

Introduction

Pandas Series is the block for handling one dimensional data [ single column ] . Just like a single column in a spreadsheet or a list of numbers

It is array- like object that can hold data of any type ( integers , float , strings ,etc ) along with cutom labels called indices

Each value has an index , making it easy to access , filter and slice

Remember : Multiple rows but in single columns

Let’s explore what Series is, why it’s important, and how to use its basic methods for data analysis.


Creating a Series

Steps :

  1. Import pandas

  2. Use pandas method named Series Example : Syntax : new_variable = pd.Series(iterable)

  3. Execute and get the output in single column

import pandas as pd

data = [2, 4, 6, 8]
s = pd.Series(data)
print(s)

Output :

0    2
1    4
2    6
3    8
dtype: int64

Another ways :

# From a list
numbers = pd.Series([10, 20, 30])

# From a dictionary
marks = pd.Series({'English': 85, 'Math': 90, 'Science': 95})

# From a scalar
zeroes = pd.Series(0, index=[1,2,3])

Basic methods in Series :

  1. Index() : To Know the index ------ All the characters are present in string format so dtype='object'

  2. Count() : Output same as size but size is property and count is method

  3. value_counts() : gives the count of values present in data

  4. unique() : to get the unique data in the array format

  5. nunique () : to get the count of unique data present in data

  6. isnull() : Get the True for null for else False

  7. notnull() : Get the False for null else True

  8. nlargest() : Orders data in descending order --- variable_name.nlargest(1) : Returns only first largest value

  9. nsmallest () : Orders data in ascending order -- variable_name.nsmallest(1) : Returns only first smallest value

  10. head () : Top n rows by default 5

  11. tail() : Bottom n rows by default 5

  12. sample() : Random n rows by default 1

  13. describe() : describes mathematical information / statistics format -- count,mean,std,min,25%,50%,75%,max

  14. info() : describes information about data -- Non-Null , Count , dtype

  15. sum() : Adds the data

  16. min () : Min of data

  17. max() : Max of data

  18. dropna () : Blank spaces or None value are removed , it will also drop the index

  19. fillna () : Blank spaces or None value are filled with value .

  20. replace() : Replace old value with new value --- If new value not given replaces with random value and generates warning

  21. sort_values () : sort the data based on values

  22. sort_index() : sort the data based on index

  23. duplicated() : #Output in boolean like True and false format --- > if duplicate element returns True else False

  24. drop_duplicated() : To drop all duplicates we going with the drop duplicates

  25. drop () : To drop multiple elements or single element variable_name.drop(position) example : d.drop(0) # to delete 1 particular number ----> variable_name.drop([[position1,position 2,... ]]) # when we want to delete multiple numbers

  26. pop() : to remove single element --- > Cannot remove multiple elements

  27. concat() : Add the data one below other ----> Limitation : concat multiple rows in series in single code —→Repeated indexing ---- >To continue the indexing use ignore_index property , set ignore_index=True

  28. To fetch particular column in data : Using Indexing : variable_name[columnindex] & Slicing : variable_name[starting_index of column:ending_index of column:step_value]


Basic proprties in Series :

  1. Index : Changing the index parameter

  2. Name : Changing the name parameter : name =" Any name "

  3. Size : To know the size of data --- To know how many data is present

  4. Shape : To know the shape of data

  5. ndim : To know the dimension of data

  6. values : To get the values

  7. dtype : Returns the datatype of data

  8. empty : Return false if data is not present in column else True

  9. axes : --- Returns the axes --- if present : [Index(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], dtype='object')] --- if not present : [RangeIndex(start=0, stop=4, step=1)]

  10. hasnas : if blank spaces or None value are present True or else false

💡
For Shape : The output will be in the form of tuple only the number of rows
💡
For values : The output will be in the form of array : The elements are not separated by comma

💡
Github repository : Check the repository for syntax

Key Takeaways

  • Series is a basic, one-dimensional data structure in Pandas.

  • Easy to create, inspect, filter, and use!

  • Learn Series before diving into more complex DataFrames.