Pandas Series

Pandas series is a One-dimensional ndarray with axis labels.The axis labels are collectively called index. It just a  column in an excel sheet.

Syntax

pandas.Series([data, index, dtype, name, copy])

where :

  • data – python object
  • index – index (axis labels) of the Series
  • dtype – dtype object of the underlying data.
  • name – Return the name of the Series.
  • copy – Return a new object with updated flags

How to Create?

Creating a Python series from list object :

import pandas as pd
a = [11, 52,44,'hello']
data = pd.Series(a)
print(data)

Using array() function :


import pandas as pd
import numpy as np
arr= np.array(['a','b','c','d','e'])
data= pd.Series(arr)
print(data)

Using Key/Value(dict) Objects as Series:

The keys of the dictionary become the labels.

import pandas as pd

a = {"a": 1, "b": 2, "c": 3}

data = pd.Series(a)

print(data)

Labels

The values are labeled with their index number and it can be used to access a specified value.

To create Labels use “index” argument.

import pandas as pd

a = [11, 57, 52,88]

data = pd.Series(a, index = ["a", "b", "c","d"])

print(data)

How to Access Elements of Series?

Indexing

To access the series element refers to the index number.The index must be an integer. In order to access multiple elements from a series, we use Slice operation.

import pandas as pd

data = ['a',2,'hello',4,5,'revin']

ser = pd.Series(data)
print(ser[0:3])

Label(index)

To access an element from series, we have to set values by index label.

import pandas as pd

data = ['a',2,'hello',4,5,'revin']

ser = pd.Series(data,index=[1,2,3,4,5,6])
print(ser[4])

Basic Selection of data

Viewing the top and bottom of the Series:

df.head()
df.tail()

loc & iloc Method

The df.loc indexer selects data in a different way than just the indexing operator. It can select subsets of data.

The df.iloc indexer is very similar to df.loc but only uses integer locations to make its selections.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *