# Data Structure-SERIES |Pandas tutorial

**Data Structure-SERIES**

**Pandas deals with 3 data structure, the higher dimensional data structure is a container of its lower-dimensional data structure. Through this article, I will cover ‘SERIES’ only.**

**1. SERIES**

It is a one- dimensional labeled array capable of holding data of any type

(integers, float, python objects, strings, etc.) the axis labels are collectively called index.

**A). Create Series:**

You can create a series using constructor given below:

```
pandas.Series(data , index , dtype , copy)
```

**Parameters:****data:** It can be of various forms like ndarray, list, constants.**index:** it must be a unique and hashable, same length as data. If no index is passed, by default it takes np.arange(n).**dtype:** It is for the data type, like string, integer, float, python objects, etc.**copy:** copy data. By default it is False.

A series can be created using inputs like:

• Array

• Dictionary

• Scalar value or constant

**B). Create an Empty Series.**

**Code:** #import the pandas library and aliasing as pd

```
import pandas as pd
s = pd.Series()
print(s)
```

**output:**

`Series([], dtype: float64)`

**C) Create a Series from ndarray without index.**

Note: if data is a ndarray then, index must be of same length.

If no index is passes then it will be range(n) where n is array length.**Code: **

```
import pandas as pd
import numpy as np
datainput = np.array(['x', 'y', 'z', 'w' ,'u'])
s=pd.Series(datainput)
print(s)
```

**output:**

```
#output:
'''
0 x
1 y
2 z
3 w
4 u
dtype: object'''
```

**D) Create a Series from ndarray with index.**

**code:**

```
import pandas as pd
import numpy as np
datainput = np.array(['x', 'y', 'z', 'w' ,'u'])
s=pd.Series(datainput, index=[70,71,72,73,74])
print(s)
```

**output:**

```
#output:
'''
70 x
71 y
72 z
73 w
74 u
dtype: object'''
```

**E).Create a Series from ‘dict’ without index.**

**code:**

```
import pandas as pd
import numpy as np
data ={'apple' : 1 ,'mango' :2 ,'orange':3 , 'grapes':4}
s=pd.Series(data)
print(s)
```

**output:**

```
#output:
'''apple 1
mango 2
orange 3
grapes 4
dtype: int64'''
```

**F). Create a Series from ‘dict’ with index.**

**code:**

```
import pandas as pd
import numpy as np
data ={'apple' : 100 ,'mango' :200 ,'orange':300 , 'grapes':400}
s=pd.Series(data, index=['apple' , 'orange' , 'grapes', 'mango','guava'])
print(s)
```

**output:**

```
#output:
'''apple 100.0
orange 300.0
grapes 400.0
mango 200.0
guava NaN
dtype: float64'''
```

Note: order is preserved.

**G). Accessing Data from Series with the position.**

Data in the series can be accessed similar to that in an array. Indexing starts from 0, i.e first element is stored at 0th position and so on.

**#Retrieve first series element.**

**code:**

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

**output:**

` 1`

**#Retrieve first three elements**

**code:**

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

**output: **

```
'''
a 1
b 2
c 3
dtype: int64'''
```

**# Retrieve last three elements ****code: **

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

**output: **

```
'''
c 3
d 4
e 5
dtype: int64'''
```

**#Retrieve single element using the label**

**code:**

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

**output: **

```
1
```

**#Retrieve multiple element using label **

**code:**

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

**output: **

```
'''
a 1
b 2
c 3
dtype: int64'''
```

**#Retrieve element when the label is not contained in the index.****I**f the label is not contained in the index then an error will be raised.

**code:**

import pandas as pd

import numpy as np

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

print(s['f'])**output: **

`KeyError: 'f'`