Dataframe NOtes
DATAFRAME-
It is a two-dimensional object that is useful in representing data in the form of rows and columns.
It is similar to a spreadsheet or an SQL table.
This is the most commonly used pandas object.
Once we store the data into the Dataframe, we can perform various operations that are useful in
analyzing and understanding the data.
PROPERTIES OF DATAFRAME
A Dataframe has axes (indices)-
➢ Row index (axis=0)
➢ Column index (axes=1)
2. It is similar to a spreadsheet , whose row index is called index and column index is called column
name.
3. A Dataframe contains Heterogeneous data.
4. A Dataframe Size is Mutable. 5. A Dataframe Data is Mutable.
A data frame can be created using any of the following
1. Series
2. Lists
3. Dictionary
4. A numpy 2D array
Iteration on Rows and Columns
If we want to access record or data from a data frame row wise or column wise then iteration is
used. Pandas provide 2 functions to perform iterations
1. iterrows ()
2. iteritems ()
iterrows() It is used to access the data row wise. Example
iteritems()
It is used to access the data column wise. Example
Select operation in data frame
To access the column data ,we can mention the column name as subscript. e.g. - df[empid] This can
also be done by using [Link].
To access multiple columns we can write as df[ [col1, col2,---] ]
To Add & Rename a column in data frame
To Delete a Column in data frame We can delete the column from a data frame by using any of the
the following –
1. del
2. pop()
3. drop()
>>del df[‘List3’]
We can simply delete a column by passing column name in subscript with df
>[Link](‘List2’) we can simply delete a column by passing column name in pop method.
To Delete a Column Using drop()
Accessing the data frame through loc() and iloc() method or indexing using Labels
Pandas provide loc() and iloc() methods to access the subset from a data frame using row/column.
Accessing the data frame through loc() It is used to access a group of rows and columns. Syntax
[Link][StartRow : EndRow, StartColumn : EndColumn]
Note -If we pass : in row or column part then pandas provide the entire rows or columns
respectively.
Accessing the data frame through iloc()
It is used to access a group of rows and columns based on numeric index value. Syntax
[Link][StartRowindexs : EndRowindex, StartColumnindex : EndColumnindex]
Note -If we pass : in row or column part then pandas provide the entire rows or columns
respectively.
head() and tail() Method
The method head() gives the first 5 rows and the method tail() returns the last 5 rows.
Example:
To display first 2 rows we can use head(2) and to returns last2 rows we can use tail(2) and to return
3rd to 4th row we can write df[2:5].
Boolean Indexing in Data Frame
Boolean indexing helps us to select the data from the DataFrames using a boolean vector. We
create a DataFrame with a boolean index to use the boolean indexing.
Filtering Data:
young_customers = df[df[‘customer_id’] < 104] print(“\nCustomers with
‘customer_id’ less than 104:\n”, young_customers) It filters the DataFrame to
include only the rows where the ‘customer_id’ is less than 104.
Adding row in a Dataframe
Using loc and at
[Link][newrow,:]=[values]
[Link][newrow,:]=<newvalue]
Modifying single cell
Df.<Columnname>[rowname/label]=new/modified value
[Link][“banglore”]=67676