DataFrame

Series object data structure are one dimensional objects. The DataFrame objects of Pandas can store two-dimensional heterogeneous data.

A DataFrame is Pandas data Structure which stores data in two- dimentional way.

It is actually a two- dimentional (tabular and spreadsheet like) labeled array , which is actually collection of columns where columns may store different types of data, eg ., numeric or string or floating point or Boolean type etc.

A DataFrame is a two-dimensional labeled array like pandas data structure, that can store data of different types. The number of elements in a 2-D array can be determined by multiplying numbers of rows and columns. For example, the number of elements in an array A [5] [4] is calculated as 5 * 4 = 20.

Characteristics of a DataFrame data structure-

  - It has two indexes (two axes)- a row index (axis=0) and a column index (axis=1)
  - It is like a spreadsheet where each value is identified with the combination of row index and     column index .
  - The indexes can be of numbers or letters or strings.
  - Its columns can have data of different types.
  - We can easily change its value, i.e , it is value-mutable .
  - We can add or delete rows/columns in a DataFrame . In others words, it is size-mutable.

Creating and displaying DataFrame -

A DataFrame object can be created by passing data in two- dimentional format.

We can create a DataFrame object by passing data in many different ways, such as:

Two-dimensional dictionaries

Two-dimensional ndarrays ( NumPy array)

Series type object

Another dataframe object   

Creating a DataFrame object from a 2-Dimensional Dictionary-

A 2-dimensioanl dictionary is a dictionary having items as ( key:value ) where value part is: another dictionary, a List, an ndarray , a Series object . But the value parts of all the keys should have similar structure and equal lengths.

Creating a DataFrame from a 2D dictionary having values as List/ ndarrays :-

We can specify our own indexes too by specifying a sequence by the name index in the DataFrame( ) function. E.g., dtf2= pd.DataFrame (dict1, index= ['I', 'II', 'III', 'IV'])

DataFrame object df1 created from 2D dictionary sales .

While creating a dataframe with a nested or 2D dictionary. Python interprets the outer dictionary keys as the columns and the inner keys as the row indices.

Here default name is given to rows index and columns starting from 0.

If the rows of ndarrays ( NumPy array) differ in length, i.e., if the number of elements in each row differ, then Python will create just single column in the dataframe object and the type of the column will be considered as object.

Creating a DataFrame object from a 2D dictionary with values as Series object-

While creating a dataframe with a nested or 2D dictionary. Python interprets the outer dictionary keys as the columns and the inner keys as the row indices.

Selecting or Accessing Data-

<DataFrame object>.<column name> or <DataFrame object>[<column name>]

If you are using dot notation , do not use quotation marks around the column name.

We can also use row and column numeric index position to access a subset, but then we will have to use iloc instead of loc . iloc means integer location .

2. We can also use at or iat attribute with DataFrame object.

 Syntax:
 <DF object>. at[<row name>, <column name>]
 <DF object>. iat [<row index no>, <column index no>]
 Eg .,
 d1.at[105,'busroute']
 d1.iat[1,3]

Inspection functions

To inspect or to get basic information about your DataFrame object, we can use info() and describe() functions.

obj1.info( )

obj1.describe( )

head () and tail ()

head () and tail () function are use to extract values from top and bottom of the dataframe object respectively.

obj1.head (4)

obj1.tail(3)

Tea Or Coffee

Python Downloads

DataFrame
Reindexing and Label Alteration
Function Application –Python pandas
Bar Charts and Histograms and Quantile
Plottint with PyPlot-2

 

Ways to Keep Your Brain Healthy