Download our e-book of Introduction To Python

Matplotlib - Subplot2grid() FunctionDiscuss Microsoft Cognitive ToolkitMatplotlib - Working with ImagesMatplotlib - PyLab moduleMatplotlib - Working With TextMatplotlib - Setting Ticks and Tick LabelsCNTK - Creating First Neural NetworkMatplotlib - MultiplotsMatplotlib - Quiver PlotPython - Chunks and Chinks View More

How can I write Python code to change a date string from "mm/dd/yy hh: mm" format to "YYYY-MM-DD HH: mm" format? Which sorting technique is used by sort() and sorted() functions of python? How to use Enum in python? Can you please help me with this error? I was just selecting some random columns from the diabetes dataset of sklearn. Decision tree is a classification algo...How can it be applied to load diabetes dataset which has DV continuous Objects in Python are mutable or immutable? How can unclassified data in a dataset be effectively managed when utilizing a decision tree-based classification model in Python? How to leave/exit/deactivate a Python virtualenvironment Join Discussion

Neha Kumawat

2 years ago

- Data Operations in Numpy
- Data Operations in Pandas

1. Pandas Series

2. Pandas DataFrame

3. Pandas Panel

Python handles data of various formats through two libraries, **Pandas **and **Numpy**. In this chapter, we will see some basic examples from each of the libraries on how to operate on data.

An N-dimensional array type called ndarray is the most important object defined in NumPy. It describes the collection of items of the same type which can be accessed using a zero-based index. by different array creation routines, an instance of ndarray class can be constructed which is described later in the tutorial. The basic ndarray is created using an array function in NumPy as follows −

Following are some examples on Numpy Data handling.

```
# more than one dimensions
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a)
```

The **output **is as follows −

```
[[1, 2]
[3, 4]]
```

```
# minimum dimensions
import numpy as np
a = np.array([1, 2, 3,4,5], ndmin = 2)
print(a)
```

The **output **is as follows −

`[[1, 2, 3, 4, 5]]`

```
# dtype parameter
import numpy as np
a = np.array([1, 2, 3], dtype = complex)
print(a)
```

The **output **is as follows −

`[ 1.+0.j, 2.+0.j, 3.+0.j]`

Pandas handle data through Series, Data Frame, and Panel. We will see some examples from each of these.

Series is a one-dimensional labeled array, it is capable of holding data of any type (integer, string, float, python objects, etc.). The collection of axis labels is called index.
A pandas Series can be created using the following constructor −

Here we create a series from a Numpy Array.

```
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data)
print(s)
```

Its** output** is as follows −

```
0 a
1 b
2 c
3 d
dtype: object
```

A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. A pandas DataFrame can be created using the following constructor −

Let us now create an indexed DataFrame using arrays.

```
import pandas as pd
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
print(df)
```

Its **output** is as follows −

```
Age Name
rank1 28 Tom
rank2 34 Jack
rank3 29 Steve
rank4 42 Ricky
```

A panel is a 3D container of data. The term Panel data is derived from econometrics and is partially responsible for the name pandas − pan(el)-da(ta)-s.

A Panel can be created using the following constructor −

In the below example we create a panel from dict of DataFrame Objects

```
#creating an empty panel
import pandas as pd
import numpy as np
data = {'Item1' : pd.DataFrame(np.random.randn(4, 3)),
'Item2' : pd.DataFrame(np.random.randn(4, 2))}
p = pd.Panel(data)
print(p)
```

Its **output** is as follows −

```
Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)
Items axis: 0 to 1
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 4
```

I hope you enjoyed reading this article and finally, you came
to know about** Python - Data Operations.**

For more such blogs/courses on data science, machine
learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.

Thanks for reading…

Happy Learning…