top of page

Data Scientist Program


Free Online Data Science Training for Complete Beginners.

No prior coding knowledge required!

Data Manipulation in Pandas.

Pandas is an open-source python library used for data analysis and manipulation. With pandas, one can get lots of information from a dataset and also make adjustments to a dataset. In this article, we will learn about some techniques used in pandas used for data manipulations.

Sorting DataFrames.

Sorting a DataFrame helps you put various parts of your data into various sections you want. The two major sort functions in pandas are:

- Sort_values(): It is used to sort columns of a DataFrame in an ascending or descending order. To sort a particular column in DataFrame, you will have to pass the name(s) of the column(s) to the function.

#sorting one column

#sorting two or more columns 
df.sort_values(['age', 'weight'])                                      

Inside of the sort_value() function, you can add parameters to make your data look the way you want. For instance, the order in which you want your data, either ascending or descending.

#sorting in ascending order
df.sort_values('age', ascending = True)

#sorting in descending order
df.sort_values('age', ascending = False)

- Sort_index(): This method is used to sort a DataFrame by index.


You can also sort by order or sort by axis.


#sort along row

#sort along column

#sort in ascending order

#sort in descending order

Merging DataFrames.

The merge() method in pandas is used to merge two DataFrames. This is normally done if the two DataFrames have one or more columns in common. Before merging two DataFrame, you should check if they have any columns in common. Here are some parameters used in the merge method:

on: column(s) to join in the two DataFrames. Column(s) must be

found in both DataFrames.

how: specifies which keys are to be included in the results. The

default setting is 'inner', but you can set it to 'outer', 'left' or 'right'.

pd.merge(dataframe_1, dataframe_2, on='column_name', how='inner')

Viewing a DataFrame.

It is important to know what a DataFrame looks like and what it entails before you start working with it. There are some functions to help you with that and they are .head(), .tail(), .shape, and .info().

- .head(): It is used to view the first few rows of a DataFrame. The

default number of rows is five, but you can change the number of

rows to view to a number of your choice by setting the parameter n

to a number(integer).

#first few rows

#viewing a number of rows(from the top)

- .tail(): It is used to view the last few rows of a DataFrame.The

default number of rows is five, but you can change the number of

rows to view to a number of your choice by setting the parameter n

to a number(integer).

#last few rows

#viewing a number of rows(from the bottom)

- .shape: It returns the number of rows and columns in a

DataFrame. These values are returned as a tuple, that is

(number_of_rows, number_of_columns). You can also check only

the number of rows or columns, by setting the index to 0 for rows

and 1 for columns.

#shape of a DataFrame
#number of rows
#number of columns

- .info(): It returns information or summary about the DataFrame.

Examples of information returned include index type, columns,

non-null values and memory usage.

Selecting Rows and Columns in a DataFrame.

Whiles working with a DataFrame you might need to select some specific data within the DataFrame, to do so you will need to know the location of that specific data. Pandas uses .iloc, .loc and .ix for data selections.

- .iloc: Used to select or get data in a specified position using the

index, specifying two arguments to iloc that is row selection and

column selection. ( df.iloc[row_selection,column_selection] )


- .loc: Used to select data by specifying the row and/or column label


df.loc['row_name', 'column_name']

- .ix: It uses both .loc and .iloc interchangeably, which is both label

index selection.

#using index

#using label

#using both index and label
df.ix[3, 'column']

Summary Statistics.

Working DataFrames in python might require you to some statistical information about your DataFrame. There are lots of statistical methods in pandas module to help you get the information you need. Some of these statistical functions are .mean(), .mode(), .median(), .min(), .max(), .std() and .var()

#calculates the mean

#find the median

#find the mode

#find minimum value

#find maximum value

#calculate the standard deviation

#calcuate the variance 


Recent Posts

See All
bottom of page