Data processing with Pandas

Pandas is an open source library that contains various operations and  data structures for manipulating numerical data and time series, it makes analyzing data easier.

The Series Data Structure


In [11]: import pandas as pd

In [12]: animals = ['Tiger', 'Bear', 'Moose']
             pd.Series(animals)

Out[12]: 0 Tiger
              1 Bear
              2 Moose
              dtype: object

In [13]: numbers = [1, 2, 3]
             pd.Series(numbers)

Out[13]: 0 1
               1 2
               2 3
               dtype: int64

In [14]: animals = ['Tiger', 'Bear', None]
             pd.Series(animals)

Out[14]: 0 Tiger
               1 Bear
               2 None
               dtype: object

In [15]: numbers = [1, 2, None]
             pd.Series(numbers)

Out[15]: 0 1.0
               1 2.0
               2 NaN
               dtype: float64

In [16]: import numpy as np
             np.nan == None

Out[16]: False

In [17]: np.nan == np.nan

Out[17]: False

In [18]: np.isnan(np.nan)

Out[18]: True

In [19]: sports = {'Archery': 'Bhutan',
                              'Golf': 'Scotland', 
                              'Sumo': 'Japan',
                            'Taekwondo': 'South Korea'}
            s = pd.Series(sports)
            s

Out[19]: Archery Bhutan
               Golf Scotland
               Sumo Japan
               Taekwondo South Korea
               dtype: object

In [20]: s.index

Out[20]: Index(['Archery', 'Golf', 'Sumo', 'Taekwondo'],                                   dtype='object')

In [21]: s = pd.Series(['Tiger', 'Bear', 'Moose'], index=['India',                       'America', 'Canada'])
             s
Out[21]: India Tiger
              America Bear
              Canada Moose
              dtype: object

In [11]: sports = {'Archery': 'Bhutan',
             'Golf': 'Scotland',
             'Sumo': 'Japan',
             'Taekwondo': 'South Korea'}
             s = pd.Series(sports, index=['Golf', 'Sumo', 'Hockey'])
             s

Out[11]: Golf Scotland
               Sumo Japan
               Hockey NaN
               dtype: object


Querying a Series




Comments