Python is the most preferred language for data scientists. It provides the greater ecosystem of a programming language and the acumen of good scientific computation libraries.
Pandas is an open-source python library that implements easy, high-performance data structures and data analysis tools. The name comes from the term ‘panel data’, which relates to multidimensional data sets found in statistics and econometrics.
Installation
To install pandas, just run pip install pandas inside Python environment. Then we can import pandas as pd.
One of the most familiar things that pandas is used for is reading in CSV files, utilising pd.read_csv. It is often the starting point for practising pandas.
pd.read_csv loads this data into a DataFrame. This can be considered as essentially a table or spreadsheet. Once loaded we can take a quick glimpse of the dataset by calling head() on the data frame.
1.Pivot Table
Pandas can be practised to produce MS Excel style pivot tables. For example, in a table, a key column which has missing values. We can impute it using mean amount of other groups.
2.Boolean Indexing
Boolean Indexing is used if user wants to filter the values of a column based on conditions from another set of columns. For instance, we want a list of all students who are not scholars and got a loan. Boolean indexing can support here.
3.Apply Function
It is one of the regularly used functions for working with data and building new variables. Apply gains some value after passing each row/column of a data frame with some function. The function can be either default or user-defined.
4.Crosstab
This function is used to get an original view of the data. The function provides scope to validate some fundamental hypothesis. For instance, one column is expected to affect the other column.
5.Merge DataFrames
Merging data frames is vital when a user has data coming from various sources to be related.
6.Sorting DataFrames
When we want to sort Pandas data frame in a particular way. When a user wants to sort pandas data frame based on the values of one or more columns or sort based on the contents of row index or row names of the panda’s data frame. Pandas data frame has two useful functions
- sort_values(): this command is used to sort pandas data frame by one or more columns
- sort_index(): this command is used to sort pandas data frame by row index
The above functions come with various options, like sorting the data frame in a specific order, place, sorting with missing values, sorting by a specific algorithm and many more.
7.Plotting
Analyzing data from different columns can be very illuminating. Pandas make doing so simple with multi-column DataFrames. By default, calling df.plot() will make pandas to over-plot all column data, with each column as a single line.
8.Cut function
Seldom numerical values make more sense if grouped together. For illustration, if we’re examining to model aeroplanes (#planes flying) with the time of the day (minutes). The specific minute of an hour might not be that appropriate for predicting air traffic as analysed to the actual period of the day like “Morning”, “Afternoon”, “Evening”, “Night”, “Late Night”. Modelling air traffic this way will be more intuitive and will bypass overfitting.
9.Impute Missing Values
Imputing relates to applying a model to restore missing values.
There are several options users can consider while replacing a missing value, for example:
- A fixed value that has meaning within the domain, such as 0, distinct from all other values.
- A value from another randomly chosen from the record.
- A mean, median or mode value replaced for the column.
- A value determined by another predictive model.
Any imputing conducted on the training dataset will have to be performed on new data in the future when predictions are required from the finalized model. This needs to be taken into factor when choosing how to impute the missing values.
For example, if one chooses to impute with mean column values, the mean column values will need to be stored to file for later exercise new data that has missing values.
Pandas provide the fillna() function for returning values with a specific value.
The post 9 Effective Pandas Techniques In Python For Data Manipulation appeared first on Analytics India Magazine.