Sitemap
Pavan kumar reddy

Pavan kumar reddy

PYTHON PANDAS

7 min readJan 7, 2024

--

Press enter or click to view image in full size

Introduction:

Pandas or Python Pandas is a library of Python which is used for data analysis.

The term Pandas is derived from “Panel data system” , which is an ecometric term for multidimentioal, structured data set ecometrics.

Now a days, Pandas has become a popular option for Data Analysis.

Pandas provides various tools for data analysis in simpler form.

Pandas is an Open Source, BSD library built for Python Programming language.

Pandas offers high performance, easy to use data structure and data analysis tools.

The main author of Pandas is Wes McKinney.

INSTALLING PANDAS

Using ‘pip’:

1.Open a Terminal or Command Prompt:

  • On Windows, press Win + R, type cmd, and press Enter.
  • On macOS or Linux, open the Terminal.

2.Install Pandas:

  • pip install pandas

Using conda:

  1. Open a Terminal or Command Prompt:
  2. Create a New Conda Environment (Optional but Recommended):
  • conda create — name myenv
  • conda activate myenv

Install Pandas:

  • conda install pandas

Features of Pandas

  • Pandas, is the most popular library in Scientific Python ecosystem for doing data analysis. Pandas is capable of many taska including

1. It can read or write in many different data formats(Integer, float, double etc).

2. It can calculate in all ways data is organized.

3. It can easily select subsets of data from bulky data sets ab=nd even combine multiple datasets together.

4. It has functionality to find anfd fill missing data.

5. It allows you to apply operations to independent groups within the data.

6. It supports reshaping of data into different forms.

7. It supports advanced time-series functionality(which is the use of a model to predict future values based on previously observed values).

8. It supports visualization by integrating matplotlib and seaborn etc libraries. Pandas is best at handling huge tabular data sets comprising different data formats.

Pandas Data Structure

“A data structure is a particular way of storing and organizing data in a computer so that it can be accessed and worked with in appropriate ways”

The pandas Series

The Series object represents one-dimensional data structures in Pandas. A series consists of two components.

  • One-dimensional data (Values)
  • Index

The series is composed of two arrays associated with each other. The main array (array of values) holds one-dimensional data to which each element is associated with a label, contained within the other array (array of labels), called the index. If you want to individually see the two arrays that make up the series, you can call index and values attributes of the series. Because a series is one dimensional, it has a single axis (dimension) — the index and the values of the index — 0, 1, 2, 3 — are called axis labels.

Series creation: Introduction

The general construct for creating a Series data structure is:

pandas. series(data,index=,dtype=,copy=)

Here, data can be one of the following:

  • A one-dimensional ndarray
  • A Python list
  • A Python dictionary
  • A scalar value

Series creation: Using a one-dimensional ndarray

Press enter or click to view image in full size

When you make a Pandas Series without picking specific labels, it defaults to using numbers (starting from 0) as labels. These numbers represent the order of items in the Series. To make it more beginner-friendly and understandable, you can set your own labels using the index parameter. Just make a list of labels that matches the length of your data, making your Series easier to work with and comprehend."

Press enter or click to view image in full size

Note:

The values in a Pandas Series can store various data types such as strings, integers, floats, and booleans.

Get Pavan kumar reddy’s stories in your inbox

Join Medium for free to get updates from this writer.

Additionally, the data type of Series values can be specified during creation using the dtype parameter, requiring a valid data type.

Series creation: Using a Python list

To create a series using a Python list, you can just pass a list to the data parameter of the Series() class constructor.

Series creation: Using a Python dictionary

To create a series using a Python dictionary, you can just pass a dictionary to the data parameter of the Series() class constructor. This time, the arrays of the index and values are filled with the corresponding keys and values of the dictionary.

Series creation: Using a scalar value

we can also create a Series from a scalar value. If you do not specify the index argument, the default index is 0. If you specify the index, the value will be repeated for specified index values.

Press enter or click to view image in full size

Selecting elements from a Series

The indexing and slicing that are applicable to NumPy arrays can be extended to the series because pandas library was built on top of NumPy.

  • You can select a single element using the index number or index label of the series.
  • You can use the slice : notation with index numbers to select a range of elements from a series.
  • You can also use a list of index numbers or index labels to select multiple elements from a series.
  • You can also use the conditions and Boolean operators to select elements from a series.
Press enter or click to view image in full size

Assigning values to the elements

Series are mutable, which means that you can change the value of an element in the series after it has been initialized.

Press enter or click to view image in full size

Operations and mathematical functions on series

  1. *Operations (+, -, , /):
  • Just like you can add, subtract, multiply, and divide individual numbers, you can perform these operations on entire series in pandas.
  • Example: result_series = series1 + series2 adds corresponding elements of two series and stores the result in a new series.

2. Mathematical Functions (e.g., np.sqrt()):

  • Functions that work on single numbers in NumPy (like square root) can be applied to entire series in pandas effortlessly.
  • Example: result_series = np.sqrt(series) calculates the square root of each element in the series and stores the results in a new series.

DataFrame creation: Introduction

A DataFrame is like a table where data is organized in rows and columns, with each column having a name and each row having a unique label, allowing for easy manipulation and analysis of structured data.

Creation:

  • You can create a DataFrame from various sources (lists, dictionaries, NumPy arrays, etc.), and it’s commonly used in data analysis tasks, providing a powerful and flexible tool for handling and exploring datasets in Python.

pd.DataFrame(data,index=,columns=)

Press enter or click to view image in full size

DataFrame creation: Using a dictionary of dictionaries

Press enter or click to view image in full size

DataFrame creation: Using a dictionary lists

Press enter or click to view image in full size

DataFrame creation: Using pandas read_*

Pandas offers a variety of file format support, including CSV, Excel, SQL, and JSON, each with corresponding read_* functions for seamlessly importing data into a DataFrame.

Press enter or click to view image in full size

The pd.read_csv() function in pandas facilitates the conversion of data from a comma-separated values (CSV) file or a plain text file into a DataFrame, enabling efficient handling and analysis of tabular information.

The pandas read_excel() function is designed to import data from an Excel file into a DataFrame, providing a seamless mechanism for working with spreadsheet-based information.

The pd.read_html() function in pandas serves the purpose of extracting and converting HTML tables into a pandas DataFrame, offering a convenient way to work with tabular data embedded in HTML documents.

The pandas read_sql() function is tailored to effortlessly fetch data from a SQL query or a database table, converting the retrieved information into a DataFrame for convenient manipulation and analysis.

pd.read_csv(filepath_or_buffer, sep=’ ,’ , header=’infer’, index_col=None, usecols=None, engine=None, skiprows=None, nrows=None)

Assigning values to the elements and adding new columns

DataFrames in pandas possess both value mutability and size mutability. This flexibility allows for the modification of existing values within the DataFrame, as well as the addition or deletion of columns, providing dynamic control over the data structure and content.

Value mutability:

Adding new rows and columns

Each column in a DataFrame can be thought of as a Series.

A Pandas Panel

A pandas Panel is like a 3D array, but it’s not commonly used, especially for beginners. It’s a bit more complex to display or visualize due to its 3D nature. Typically, Panels are employed for handling 3D time-series data. The three axes in a Panel are named as follows:

  • items: Think of this as the first dimension. Each item represents a DataFrame.
  • major_axis: This is the second dimension. Each item corresponds to the rows of the DataFrame.
  • minor_axis: The third dimension. Each item corresponds to the columns of each DataFrame.

--

--