Header Ads Widget

Responsive Advertisement

Data handling Using Pandas : How to define series and dataframe


Introduction to Python Libraries : Python libraries contain a collection of builtin modules that allow us to perform many actions without writing detailed programs for it. Each library in Python contains a large number of modules that one can import and use. NumPy, Pandas and Matplotlib are three well-established Python libraries for scientific and analytical use. These libraries allow us to manipulate, transform and visualise data easily and efficiently. NumPy, which stands for ‘Numerical Python’, is a library we discussed in class XI. Recall that, it is a package that can be used for numerical data analysis and scientific computing. NumPy uses a multidimensional array object and has functions and tools for working with these arrays. Elements of an array stay together in memory, hence, they can be quickly accessed. In this section we will about how to create series using pandas.


In this chapter we will learn about the important library PANDAS. PANDAS (PANel DAta) is a high-level data manipulation tool used for analysing data. It is very easy to import and export data using Pandas library which has a very rich set of functions. It is built on packages like NumPy and Matplotlib and gives us a single, convenient place to do most of our data analysis and visualisation work. Pandas has three important data structures, namely – Series, DataFrame and Panel to make the process of analysing data organised, effective and efficient.

The Matplotlib library in Python is used for plotting graphs and visualisation. In this chapter we will learn about only Pandas Matplotlib library and Numpy we will discuss in upcoming articles. In this article we will just focus on PANDAS library.

Installing Pandas Installing Pandas is very similar to installing NumPy. To install Pandas from command line, we need to type in: 

pip install pandas 

Note that both NumPy and Pandas can be installed only when Python is already installed on that system. The same is true for other libraries of Python. Steps to install pandas in You Python. There are several methods to install PANDAS in python. we will discuss some of them to install pandas in python. Click here to Know how to install pandas in Pyhton

Data Structure in Pandas 

A data structure is a collection of data values and operations that can be applied to that data. It enables efficient storage, retrieval and modification to the data. For example, we have already worked with a data structure ndarray in NumPy in Class XI. Recall the ease with which we can store, access and update data using a NumPy array. Two commonly used data structures in Pandas that we will cover in this book are: 

  • Series 
  • DataFrame
Series 
A Series is a one-dimensional array containing a sequence of values of any data type (int, float, list, string, etc) which by default have numeric data labels starting from zero. The data label associated with a particular value is called its index. We can also assign values of other data types as index. We can imagine a Pandas Series as a column in a spreadsheet. Example of a series containing names of students is given below:
Index    Value 
0           Arnab 
1           Samridhi 
2           Ramit 
3           Divyam 
4            Kritika

How to create Series in Python
Creation of Series from Scalar 
Values A Series can be created using scalar values as shown in the example below: 

>>> import pandas as pd      #import Pandas with alias pd 

>>> series1 = pd.Series([10,20,30])     #create a Series 

>>> print(series1)      #Display the series

 Index    Value 

0           Arnab 
1           Samridhi 
2           Ramit 
3           Divyam 
4            Kritika


Observe that output is shown in two columns - the index is on the left and the data value is on the right. by default indices range from 0 through N – 1 for N  number of data.

The following example has a numeric index in random order.

 >>> series2 = pd.Series(["Kavi","Shyam","Ra vi"], index=[3,5,1]) 

>>> print(series2) #Display the series 

Output: 

3      Kavi 

5      Shyam 

1     Ravi 

dtype: object


We can also use letters or strings as indices, for example: 

>>> series2 = pd.Series([2,3,4],index=["Feb","M ar","Apr"]) 

>>> print(series2) #Display the series 

Output: 

Feb 2 

Mar 3 

Apr 4 

dtype: int64


Creation of Series from NumPy Arrays 

We can create a series from a one-dimensional (1D) NumPy array, as shown below: Activity 2.1 Create a series having names of any five famous monuments of India and assign their States as index values. 

>>> import numpy as np # import NumPy with alias np

>>> import pandas as pd 

>>> array1 = np.array([1,2,3,4]) 

>>> series3 = pd.Series(array1) 

>>> print(series3)

Output: 

0 1 

1 2 

2 3 

3 4 

dtype: int32

We can also define Index as follows:

>>> import numpy as np # import NumPy with alias np

>>> import pandas as pd 

>>> array1 = np.array([1,2,3,4]) 

 >>> series4 = pd.Series(array1, index = ["Jan", "Feb", "Mar", "Apr"])

 >>> print(series4) 

Out Put of the above code:

Jan 1 

Feb 2 

Mar 3 

Apr 4 

dtype: int32


Creation of Series from Dictionary 

Recall that Python dictionary has key: value pairs and a value can be quickly retrieved when its key is known. Dictionary keys can be used to construct an index for a Series, as shown in the following example. Here, keys of the dictionary dict1 become indices in the series. 

>>> dict1 = {'India': 'NewDelhi', 'UK': 'London', 'Japan': 'Tokyo'} 

>>> print(dict1) #Display the dictionary {'India': 'NewDelhi', 'UK': 'London', 'Japan': 'Tokyo'}

 >>> series8 = pd.Series(dict1) 

>>> print(series8) #Display the series

Out Put of the above code:

India New Delhi 

UK London 

Japan Tokyo 

dtype: object

 

In this article we learn how we can define series in python with pandas. We will continue this chapter, in next article we will discuss about How we can access this series and different method. 

Click here to go Next Article.




What is pandas in Python with example? What are pandas series in Python? What is NumPy and pandas in Python? pandas in python install pandas in python tutorial pandas in python example pandas in python w3schools pandas in python methods pandas in python geeksforgeeks pandas in python interview questions pandas in python javatpoint How do I install pandas in Python? Can I pip install pandas? How do I download pandas in Python idle? Is pandas already in Python? install pandas in python windows how to install pandas in python idle pip install pandas python 3 install pandas in pycharm install pandas in jupyter notebook how to import pandas in python conda install pandas pip install pandas error ncert ip book class 12 python ncert ip book class 12 solutions pdf ncert books class 12 ncert ip book class 11 solutions pdf informatics practices class 11 book pdf informatics practices class 12 ncert textbook pdf download 2020-21

Post a Comment

0 Comments