Python Pandas – Series

Series is one-dimensional labeled array capable of holding any data type such as integers, strings, floating-point numbers, Python objects, etc. The axis labels are collectively referred to as the index.

Parameters:

  • data : Contains data stored in Series.
  • index : Index values must be unique and hashable, the same length as data.
        • Default (0,1,2,….,n) if no index is passed.
  • dtype : Data type for the output Series.
  • copy : bool(Default-False). Copy input data

Create Pandas Series

A Series accepts three types of input:

  • Python dict
  • Array
  • Scalar value

 

1. Create a Series from Array:

If data is an ndarray, index must be the same length as data. If no index is passed, one will be created having values [0, …, len(data)-1].

Example:

import pandas as pd
import numpy as np
 
s1 = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
s2 = pd.Series(np.random.randn(5))
 
print("Series with Index:\n",s1)
print("\nSeries with Deafult Index:\n",s2)

This produces the following result:

Series with Index:
a   -1.759479
b    0.846408
c   -3.072220
d   -1.358553
e    0.458533
dtype: float64

Series with Deafult Index:
0   -1.994786
1    0.210055
2   -0.335209
3   -0.433028
4   -0.640973
dtype: float64

2. Create a Series from Dict:

When dict is the input of Series and an index is not passed, the Series index will be ordered by the dict’s insertion order,  if you’re using Python version >= 3.6 and Pandas version >= 0.23.

If you’re using Python < 3.6 or Pandas < 0.23, and an index is not passed, the Series index will be the lexically ordered list of dict keys.

Example:

import pandas as pd
import numpy as np
 
d = {'b': 1, 'a': 0, 'c': 2}
s1 = pd.Series(d)
s2 = pd.Series(d, index=['b', 'c', 'd', 'a'])
 
print("Series without Index:\n",s1)
print("\nSeries with Index:\n",s2)

Output:

Series without Index:
a    0
b    1
c    2
dtype: int64

Series with Index:
b    1.0
c    2.0
d    NaN
a    0.0
dtype: float64
Note: NaN (not a number) is the standard missing data marker used in pandas.

3. Create a Series from Scalar Value:

If data is a scalar value, an index must be provided. The value will be repeated to match the length of the index.

Example

import pandas as pd
s1 = pd.Series(5., index=['a', 'b', 'c', 'd', 'e'])
print("s1:\n",s1)

Output:

s1:
a    5.0
b    5.0
c    5.0
d    5.0
e    5.0
dtype: float64

Accessing Data from Series with Position

Series acts very similarly to a numpy’s ndarray, and is a valid argument to most NumPy functions. However, operations such as slicing will also slice the index.

import pandas as pd
import numpy as np
 
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
 
print("Series:\n",s)
print("\nFirst element of Series :",s[0])
print("\nSlicing of Series:\n",s[:2])
 
log_s = np.log(s)
print("\n logarithmic Series:\n",log_s)

Output:

Series:
a    1
b    2
c    3
d    4
e    5
dtype: int64

First element of Series : 1

Slicing of Series:
a    1
b    2
dtype: int64

logarithmic Series:
a    0.000000
b    0.693147
c    1.098612
d    1.386294
e    1.609438
dtype: float64

Accessing Data Using Label (Index):

A Series is like a fixed-size dict in that you can get and set values by index label.

import pandas as pd
import numpy as np
 
d = {'b': 10, 'a': 5, 'c': 2,'d':8}
s = pd.Series(d, index=['b', 'c', 'd', 'a'])
 
print(s['a'])
print(s['d'])
print(s['f'])

This produces the following result:

5
8
...
KeyError: 'f'

Check Index present in Series

You can check the specified index is present or not in series using Python’s membership operator in and not in.

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

if 'e' in s:
    print("Index e is present in Series")

print('f' in s)
# Output
Index e is present in Series
False

Convert to Numpy array

You can convert pandas series to numpy array bu using series.to_numpy() function.

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
arr = s.to_numpy()
arr
# Output
array([1, 2, 3, 4, 5])

.     .     .

Leave a Reply

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

Python Pandas Tutorials

Pandas – How to remove DataFrame columns with constant (same) values?

Pandas – How to remove DataFrame columns with only one distinct value?

Pandas – Count unique values for each column of a DataFrame

Pandas – Count missing values (NaN) for each columns in DataFrame

Pandas – MultiIndex

Pandas – Applymap

Pandas – Apply

Pandas – Map

Pandas – Missing Data

Difference between Merge, join, and concatenate

Pandas – Join

pandas : Handling Duplicate Data

Pandas : Handling Categorical Data

Pandas : Data Types

Appending a row to DataFrame

Python Pandas – Merge

Python Pandas – Concatenation & append

Python Pandas – GroupBy

Python Pandas – Visualization

Python Pandas – Options and Customization

Python Pandas – Descriptive Statistics

Python Pandas – Basic functions

Python Pandas – DataFrame

Python Pandas – Introduction

Study Machine Learning