0% found this document useful (0 votes)
3 views21 pages

13 Lecture Student

The document provides an overview of programming for engineers, focusing on scientific computing with SciPy and data handling using the Pandas library. It covers key functionalities such as loading data into DataFrames, accessing and modifying data, analyzing data, and presenting data through plotting with Matplotlib. The document emphasizes the importance of NumPy for SciPy and the integration of Pandas for efficient data manipulation and analysis.

Uploaded by

elifgulusen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views21 pages

13 Lecture Student

The document provides an overview of programming for engineers, focusing on scientific computing with SciPy and data handling using the Pandas library. It covers key functionalities such as loading data into DataFrames, accessing and modifying data, analyzing data, and presenting data through plotting with Matplotlib. The document emphasizes the importance of NumPy for SciPy and the integration of Pandas for efficient data manipulation and analysis.

Uploaded by

elifgulusen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Programming for Engineers

Assist. Prof. Rezan BAKIR


S T A R T
13.Lecture Outline
Scientific and Engineering Libraries
Scientific Computing with SciPy
SciPy is a library that includes many methods and facilities for Scientific Computing. It is closely
linked with NumPy so much that NumPy needs to be imported first to be able to use SciPy
pip install scipy
Data handling & analysis with Pandas
$ pip install pandas.
Supported File Formats
Data handling & analysis with Pandas
Data Frames : Pandas library is built on top of a data type called DataFrame which is used to store
all types of data while working with Pandas.
1. Loading data from a file.

If you have your data in a file


that is supported by Pandas,
you can use its reader for
directly obtaining a DataFrame
object
Data handling & analysis with Pandas
1. Loading data from a file.

• read_csv file automatically understood that our CSV file had a header (“Name”, “Grade” and
“Age”).
If your file does not have a header, you can call read_csv with the header parameter set to None
as follows: pd.read_ csv(filename, header=None).

• read_csv read all columns in the CSV file. If you wish to load only some of the columns
(e.g. ‘Name’, ‘Age’ in our example), you can relay this using the usecols parameter as follows:
pd. read_csv (filename, usecols=[ 'Name', 'Age']).
Data handling & analysis with Pandas
2. Convert Python data into a DataFrame
Data handling & analysis with Pandas

In many cases, we will


require the rows to be
associated with names, or
sometimes called as keys.
For example, instead of
referring to a row as “the row
at index 1”, we might require
accessing a row with a
non-integer value.
Data handling & analysis with Pandas
Alternatively, we can obtain a DataFrame
from a dictionary, which might be easier
for us to create data column-wise – note
that the column names are obtained from
the keys of the dictionary
Data handling & analysis with Pandas
Accessing Data with DataFrames
1. Columnwise access.

For columnwise access, you can


use df["Grade"], which returns a
sequence of values.
To access a certain element in that
sequence, you can either use an
integer index (as in df["Grade"][1])
or named index if it has been
defined (as in
df["Grade"]["Amanda"]).
Data handling & analysis with Pandas
Accessing Data with DataFrames
1. 2. Rowwise access.
To access a particular row, you can either use integer indexes with df.iloc[<row index>] or
df.loc[<row name>] if a named index is defined.
Data handling & analysis with Pandas
Accessing Data with DataFrames
You can also provide both column index and name index in a single operation, i.e. [<row index>,
<column index>] or [<row name>, <column name>]

While accessing a DataFrame with integer


indexes, you can also use Python’s slicing;
i.e. [start:end:step] indexing
Data handling & analysis with Pandas
Modifying Data with DataFrames

!! This is called chained indexing


and Pandas will give you a warning
that you are modifying a DataFrame
that was returned while accessing
columns or rows in your original
DataFrame: df['Grade'] returns a
direct access for the original Grade
column or a copy to it and
depending on how your data is
structured and stored in the
memory, the end result may be
different and your change might not
be reflected on the original
DataFrame.
Data handling & analysis with Pandas
Analyzing Data with DataFrames

Pandas provides functions for sorting (using .sort_values() function), finding the maximum or the minimum (using .max() or
.min() functions) or finding the largest or smallest n values (using .nsmallest() or .nlargest() functions
Data handling & analysis with Pandas
Presenting Data in DataFrames
Pandas provides a very easy mechanism for plotting your data via .plot() function
Data handling & analysis with Pandas
Presenting Data in DataFrames
Plotting data with Matplotlib $ pip install matplotlib.
1. Parts of a Figure
A figure consists of the following elements :
- Title of the figure. - Axes, together with their ticks, tick labels,
and axis labels. - The canvas of plot, which consists of a
drawing of your data in the form of a dots (scatter plot), lines
(line plot), bars (bar plot), surfaces etc. - Legend, which informs
the perceiver about the different plots in the canvas.
Data handling & analysis with Pandas
Preparing your Data for Plotting
Matplotlib expects numpy arrays as input and therefore, if you have your data in a numpy array, you can directly plot it
without any data type conversion. With pandas DataFrame, the behavior is not guaranteed and therefore, it is recommended
that the values in a DataFrame are first converted to a numpy array
Data handling & analysis with Pandas
Drawing Single Plots There are two ways to plot with matplotlib: In an object oriented style or Pyplot style
Data handling & analysis with Pandas
Drawing Single Plots There are two ways to plot with matplotlib: In an object oriented style or the so-called Pyplot style
Data handling & analysis with Pandas
Drawing Multiple Plots in a Figure
Thank You

You might also like