DataFrame
Last updated
Last updated
Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. Pandas DataFrame consists of three principal components, the data, rows, and columns.
We will get a brief insight on all these basic operation which can be performed on Pandas DataFrame :
Creating a Pandas DataFrame
In the real world, a Pandas DataFrame will be created by loading the datasets from existing storage, storage can be SQL Database, CSV file, and Excel file. Pandas DataFrame can be created from the lists, dictionary, and from a list of dictionary etc. Dataframe can be created in different ways here are some ways by which we create a dataframe:
Creating a dataframe using List: DataFrame can be created using a single list or a list of lists.
For more details refer to Creating a Pandas DataFrame
Dealing with Rows and Columns
A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. We can perform basic operations on rows/columns like selecting, deleting, adding, and renaming.
Column Selection: In Order to select a column in Pandas DataFrame, we can either access the columns by calling them by their columns name.
Indexing and Selecting Data
Indexing in pandas means simply selecting particular rows and columns of data from a DataFrame. Indexing could mean selecting all the rows and some of the columns, some of the rows and all of the columns, or some of each of the rows and columns. Indexing can also be known as Subset Selection.
Indexing a Dataframe using indexing operator []
:
Indexing operator is used to refer to the square brackets following an object. The .loc
and .iloc
indexers also use the indexing operator to make selections. In this indexing operator to refer to df[].
Selecting a single columns
In order to select a single column, we simply put the name of the column in-between the brackets
Selecting a single row
In order to select a single row using .loc[]
, we put a single row label in a .loc
function.
Selecting a single row
In order to select a single row using .iloc[]
, we can pass a single integer to .iloc[]
function.
Working with Missing Data
Missing Data can occur when no information is provided for one or more items or for a whole unit. Missing Data is a very big problem in real life scenario. Missing Data can also refer to as NA(Not Available) values in pandas.
Checking for missing values using isnull()
and notnull() :
In order to check missing values in Pandas DataFrame, we use a function isnull()
and notnull()
. Both function help in checking whether a value is NaN
or not. These function can also be used in Pandas Series in order to find null values in a series.
Iterating over rows and columns
Iteration is a general term for taking each item of something, one after another. Pandas DataFrame consists of rows and columns so, in order to iterate over dataframe, we have to iterate a dataframe like a dictionary.
Iterating over rows :
In order to iterate over rows, we can use three function iteritems()
, iterrows()
, itertuples()
. These three function will help in iteration over rows.
Run on IDE
Run on IDE
DataFrame Methods:
FUNCTION | DESCRIPTION |
index() | Method returns index (row labels) of the DataFrame |
Method inserts a column into a DataFrame | |
Method returns addition of dataframe and other, element-wise (binary operator add) | |
Method returns subtraction of dataframe and other, element-wise (binary operator sub) | |
Method returns multiplication of dataframe and other, element-wise (binary operator mul) | |
Method returns floating division of dataframe and other, element-wise (binary operator truediv) | |
unique() | Method extracts the unique values in the dataframe |
Method returns count of the unique values in the dataframe | |
value_counts() | Method counts the number of times each unique value occurs within the Series |
columns() | Method returns the column labels of the DataFrame |
axes() | Method returns a list representing the axes of the DataFrame |
Method creates a Boolean Series for extracting rows with null values | |
Method creates a Boolean Series for extracting rows with non-null values | |
between() | Method extracts rows where a column value falls in between a predefined range |
Method extracts rows from a DataFrame where a column value exists in a predefined collection | |
dtypes() | Method returns a Series with the data type of each column. The resultâs index is the original DataFrameâs columns |
Method converts the data types in a Series | |
values() | Method returns a Numpy representation of the DataFrame i.e. only the values in the DataFrame will be returned, the axes labels will be removed |
Method sorts a data frame in Ascending or Descending order of passed Column | |
Method sorts the values in a DataFrame based on their index positions or labels instead of their values but sometimes a data frame is made out of two or more data frames and hence later index can be changed using this method | |
Method retrieves rows based on index label | |
Method retrieves rows based on index position | |
Method retrieves DataFrame rows based on either index label or index position. This method combines the best features of the .loc[] and .iloc[] methods | |
Method is called on a DataFrame to change the names of the index labels or column names | |
columns() | Method is an alternative attribute to change the coloumn name |
Method is used to delete rows or columns from a DataFrame | |
Method is used to delete rows or columns from a DataFrame | |
Method pulls out a random sample of rows or columns from a DataFrame | |
Method pulls out the rows with the smallest values in a column | |
Method pulls out the rows with the largest values in a column | |
Method returns a tuple representing the dimensionality of the DataFrame | |
Method returns an âintâ representing the number of axes / array dimensions. Returns 1 if Series, otherwise returns 2 if DataFrame | |
Method allows the user to analyze and drop Rows/Columns with Null values in different ways | |
Method manages and let the user replace NaN values with some value of their own | |
Values in a Series can be ranked in order with this method | |
Method is an alternate string-based syntax for extracting a subset from a DataFrame | |
Method creates an independent copy of a pandas object | |
Method creates a Boolean Series and uses it to extract rows that have duplicate values | |
Method is an alternative option to identifying duplicate rows and removing them through filtering | |
Method sets the DataFrame index (row labels) using one or more existing columns | |
Method resets index of a Data Frame. This method sets a list of integer ranging from 0 to length of data as index | |
Method is used to check a Data Frame for one or more condition and return the result accordingly. By default, the rows not satisfying the condition are filled with NaN value |
More on Pandas
Reference : https://www.geeksforgeeks.org/python-pandas-dataframe/
Output: Creating DataFrame from dict of ndarray/lists: To create DataFrame from dict of narray/list, all the narray must be of same length. If index is passed then the length index should be equal to the length of arrays. If no index is passed, then by default, index will be range(n) where n is the array length.
Output:
Output:
Row Selection: Pandas provide a unique method to retrieve rows from a Data frame. DataFrame.loc[]
method is used to retrieve rows from Pandas DataFrame. Rows can also be selected by passing integer location to an iloc[] function.
Note: Weâll be using nba.csv
file in below examples.
Output: As shown in the output image, two series were returned since there was only one parameter both of the times. For more Details refer to Dealing with Rows and Columns
Output:
Indexing a DataFrame using .loc[ ]
:
This function selects data by the label of the rows and columns. The df.loc
indexer selects data in a different way than just the indexing operator. It can select subsets of rows or columns. It can also simultaneously select subsets of rows and columns.
Output:
As shown in the output image, two series were returned since there was only one parameter both of the times.
Indexing a DataFrame using .iloc[ ]
:
This function allows us to retrieve rows and columns by position. In order to do that, weâll need to specify the positions of the rows that we want, and the positions of the columns that we want as well. The df.iloc
indexer is very similar to df.loc
but only uses integer locations to make its selections.
Output: For more Details refer
Output:
Filling missing values using fillna()
, replace()
and interpolate() :
In order to fill null values in a datasets, we use fillna()
, replace()
and interpolate()
function these function replace NaN values with some value of their own. All these function help in filling a null values in datasets of a DataFrame. Interpolate() function is basically used to fill NA
values in the dataframe but it uses various interpolation technique to fill the missing values rather than hard-coding the value.
Output:
Dropping missing values using dropna()
:
In order to drop a null values from a dataframe, we used dropna()
function this fuction drop Rows/Columns of datasets with Null values in different ways.
Now we drop rows with at least one Nan value (Null value)
Output: For more Details refer to Working with Missing Data in Pandas
Now we apply iterrows()
function in order to get a each element of rows.
Output: Iterating over Columns : In order to iterate over columns, we need to create a list of dataframe columns and then iterating through that list to pull out the dataframe columns.
Now we iterate through columns in order to iterate through columns we first create a list of dataframe columns and then iterate through list.
Output: For more Details refer to Iterating over rows and columns in Pandas DataFrame