Pandas series with program examples

Spread the love

Pandas Series:

It is one directional array, It can create with integer, float, complex, string values. The default index starts with zero same like arrays.

Code:

import numpy as np import pandas as pd import matplotlib.pyplot as plt d= pd.Series() print("Empty Series\n", d) d= pd.Series([1,2,3,6]) print("\nSeries with index", d) d= pd.Series([1,2,3,6],index=['a','b','c','d']) print("\nSeries with defined index",d)

 

Output:

Empty Series

Series([], dtype: float64)

Series with index 0    1

1    2

2    3

3    6

dtype: int64

Series with defined index a    1

b    2

c    3

d    6

dtype: int64

 

From Numpy arrays:

We can create series of values using numpy arrays. The index is automatically created based on the values.

Code:

import numpy as np import pandas as pd a=np.random.rand(10) print(a) s=pd.Series(a) print(s) print("\nprinting values only ",s.values) print("\nprinting index only ",s.index)

 

Output:

[0.31835121 0.19004467 0.50427098 0.3441177  0.97852423 0.79431625

0.98212003 0.15386787 0.1130424  0.75999688]

0    0.318351

1    0.190045

2    0.504271

3    0.344118

4    0.978524

5    0.794316

6    0.982120

7    0.153868

8    0.113042

9    0.759997

dtype: float64

printing values only  [0.31835121 0.19004467 0.50427098 0.3441177  0.97852423 0.79431625

0.98212003 0.15386787 0.1130424  0.75999688]

printing index only  RangeIndex(start=0, stop=10, step=1)

 

From Dictionary:

We can create series from dictionaries also. The key , values are assigned to index, values vice versa

Example Code:

import numpy as np import pandas as pd a= {'a': 5,'b':6, 'c':8 } print(a) s=pd.Series(a) print(s) print("\nprinting values only ",s.values) print("\nprinting index only ",s.index)

Output:

{'a': 5, 'b': 6, 'c': 8}

a    5

b    6

c    8

dtype: int64

printing values only  [5 6 8]

printing index only  Index(['a', 'b', 'c'], dtype='object')

 

From Scalar:

We can create series from using scalar value. But we need to provide the index values must.

Example Code:

import numpy as np import pandas as pd s=pd.Series(6,index=[0,1,2,3,4,5]) print(s)

Output:

0    6

1    6

2    6

3    6

4    6

5    6

dtype: int64

 

Accessing Values from Series:

We can access data from series same like ndarray.

import numpy as np import pandas as pd d= pd.Series([1,2,3,6],index=['a','b','c','d']) print('\nPrint series of values', d) print('\nAccessing first value of series', d[0]) print('\nAccessing last value of series', d[3]) print('\nAccessing first two of series', d[:2]) print('\nAccessing last two of series', d[-2:])

Output:

Print series of values a    1

b    2

c    3

d    6

dtype: int64

Accessing first value of series 1

Accessing last value of series 6

Accessing first 2 of series a    1

b    2

dtype: int64

Accessing last value of series c    3

d    6

dtype: int64

 

admin

admin

Leave a Reply

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