Pandas DataFrames

Pandas DataFrame is a 2 dimensional data structure like a table with rows and columns. It can  be thought of as a dict-like container for Series objects

Syntax:

pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=None)

where:

  • data – ndarray, Iterable, dict, or DataFrame
  • index – array of keys
  • columns – column name 
  • dtype – datatype of each python object

How to Create?

Pandas dataframe can be created by passing any iterable python object to dataframe function.

FEW EXAMPLES

Using List:

import pandas as pd

data = [1,2,3,4,5,'a','b','c','d','e']

df = pd.DataFrame(data)
print(df)

Using dictionary:

import pandas as pd

data = ['Name' :['praveen','samra','ruman','sanjay'],
'Place':['Delhi','Varanasi','UP','UK']]

df = pd.DataFrame(data)
print(df)

Access & Selection

To retrieve  rows from dataframe use either loc or iloc method.

loc is used when we know the name or labels of rows & columns.

df.loc[rowsRange : columnRange]

iloc is used when we pass an integer index to select specific row/column.

df.iloc[IntegerRowsRange : IntegerColumnRange]

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *