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

Python Unit 4.Notes

The document discusses interactive plotting with Pyplot in Matplotlib, highlighting the use of interactive backends and widgets for real-time data manipulation and visualization. It covers various types of plots such as bar graphs, line graphs, boxplots, and scatterplots, along with advanced techniques like dual-axis charts and logarithmic scales. Additionally, it emphasizes the importance of exploratory data analysis and the benefits of advanced graphical outputs for improved data understanding and decision-making.

Uploaded by

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

Python Unit 4.Notes

The document discusses interactive plotting with Pyplot in Matplotlib, highlighting the use of interactive backends and widgets for real-time data manipulation and visualization. It covers various types of plots such as bar graphs, line graphs, boxplots, and scatterplots, along with advanced techniques like dual-axis charts and logarithmic scales. Additionally, it emphasizes the importance of exploratory data analysis and the benefits of advanced graphical outputs for improved data understanding and decision-making.

Uploaded by

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

plotting - an interactive session with pyplot

An interactive session with Pyplot, a module in the Matplotlib library, enables dynamic
exploration and manipulation of plots. This approach is particularly useful for data analysis
and visualization, allowing users to observe the effects of changes in real-time.
Interactive Backends
Matplotlib supports several interactive backends, which determine how plots are displayed
and how users can interact with them. Common backends include:
 TkAgg: Uses Tkinter.
 QtAgg: Uses PyQt or PySide.
 nbAgg: or %matplotlib notebook: For use in classic Jupyter Notebook.
 widget: or %matplotlib widget: For use in Jupyter Notebooks and JupyterLab,
requires ipympl.
To enable an interactive backend, use the %matplotlib magic command in Jupyter
environments or plt.ion() in other environments before creating any plots.
Basic Interactive Plotting
Here's an example of creating a simple interactive plot:
import matplotlib.pyplot as plt
import numpy as np

# Enable interactive mode


plt.ion()

# Create a figure and axes


fig, ax = plt.subplots()

# Generate initial data


x = np.linspace(0, 10, 100)
y = np.sin(x)

# Plot the initial data


line, = ax.plot(x, y)

# Add interactivity
ax.set_title('Interactive Sine Wave')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')

# Function to update the plot


def update_plot(frequency, amplitude):
line.set_ydata(amplitude * np.sin(frequency * x))
fig.canvas.draw_idle()
# Example of updating the plot
update_plot(2, 1.5)

# Keep the plot open until closed manually


plt.show(block=True)
This code creates a sine wave plot that can be updated by calling the update_plot function
with new frequency and amplitude values. The fig.canvas.draw_idle() command redraws the
plot with the updated data.
Interactive Widgets
For more advanced interaction, Matplotlib can be combined with libraries like ipywidgets to
create interactive controls such as sliders and buttons.
Python
import matplotlib.pyplot as plt
import numpy as np
import ipywidgets as widgets
from ipywidgets import interact, interactive, fixed, interact_manual

# Enable interactive mode (if not already enabled)


plt.ion()

# Create initial data


x = np.linspace(0, 10, 100)

# Create the figure and axes


fig, ax = plt.subplots()
line, = ax.plot(x, np.sin(x))

# Define the update function


def update(frequency=1.0, amplitude=1.0):
ax.clear()
ax.plot(x, amplitude * np.sin(frequency * x))
ax.set_title('Interactive Sine Wave')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
fig.canvas.draw_idle()

# Create interactive widgets


interact(update, frequency=(0.1, 5.0, 0.1), amplitude=(0.5, 2.0, 0.1))

plt.show(block=True)
This example uses ipywidgets.interact to create sliders for adjusting the frequency and
amplitude of the sine wave. The update function redraws the plot whenever the slider values
change.

Basic plotting
We often communicate complex information through pictures, diagrams, maps, plots and
infographics. When starting to analyse a dataset, simple graphical plots are useful to sanity
check data, look for patterns and relationships, and compare different groups.
Exploratory data analysis (EDA) is described by Tukey (1977) as detective work; assembling
clues and evidence relevant to solving the case (question). It involves both visualisation of
data in various graphical plots and looking at summary statistics such as range, mean,
quartiles and correlation coefficient. The aim is to discover patterns and relationships within a
dataset so that these can be investigated more deeply by further data analysis.
A graphical plot gives a visual summary of some part of a dataset. In this step, we will
consider structured data, such as a table of rows and columns (like a spreadsheet), and how
to build some simple graphical plots using the fantastic Matplotlib library for Python (which
is already installed with Jupyter Notebook).
Structured data and variables
When looking at structured data, a row in the table corresponds to a case (also called a
record, example, instance or observation) and a column corresponds to a variable (also called
a feature, attribute, input or predictor).
When it comes to variables, it is essential to distinguish between categorical
variables (names or labels) and quantitative variables (numerical values with magnitude) to
ensure we plot the correct type of plot for that variable. For example, in crime data, ‘type of
offence’ (criminal damage, common assault, etc) would be a categorical variable, whereas in
health data, the ‘resting heart rate’ of an adult would be a quantitative variable.
Bar graph
A bar graph (or bar chart) is useful for plotting a single categorical variable. For example,
we can plot a bar graph (using plt.bar) showing the population of countries using data
from Countries in the world by population (2020).
import matplotlib.pyplot as plt
country = ['China', 'India', 'United Sates', 'Indonesia', 'Pakistan', 'Brazil']
population = [1439323776, 1380004385, 331002651, 273523615, 220892340, 212559417]
plt.bar(country,population)
plt.title('Population of Countries (2020)')
plt.xlabel('Country')
plt.ylabel('Population')
plt.show()
The bar graph produced by the Python code above looks like the image below. Note that the
‘1e9’ at the top of the population axis indicates that the values on that axis are in units of
(1times10^9) which is 1 billion. Notice the presence of the title and axis labels.

(Population of Countries, 2020)


If we replace plt.bar with plt.barh we get a nice horizontal bar graph instead, as in the image
below. Try this for yourself.
(Population of Countries, 2020)
Line graph
A line graph is useful for plotting a quantitative variable that changes over time. For
example, we can plot a line graph (using plt.plot) showing the population of the United
Kingdom using data from the Office for National Statistics.
import matplotlib.pyplot as plt
year = [1851, 1861, 1871, 1881, 1891,
1901, 1911, 1921, 1931, 1941,
1951, 1961, 1971, 1981, 1991,
2001, 2011]
population = [ 27368800, 28917900, 31484700, 34934500, 37802400,
41538200, 42189800, 43904100, 46073600, 44870400,
50286900, 52807400, 55928000, 56357500, 57438700,
59113016, 63285145 ]
plt.plot(year, population)
plt.title('Population of United Kingdom')
plt.xlabel('Year')
plt.ylabel('Population')
plt.show()
The line graph produced by the Python code above looks like the image below.
Boxplot
A boxplot is useful for plotting a single quantitative variable. It shows the five-number
summary of a dataset visually. The five numbers are the minimum, lower quartile, median,
upper quartile and maximum.

For example, using the dataset for number of daily births in the USA (2000-2014) used in a
previous step, we could write the following Python code to plot a boxplot (using plt.boxplot)
of the data.
import pandas as pd
url = 'https://raw.githubusercontent.com/fivethirtyeight/data/master/births/US_births_2000-
2014_SSA.csv'
mydata = pd.read_csv(url)
plt.boxplot(mydata.births, vert=False)
plt.title('Distribution of Number of Daily Births in the USA (2000-2014)')
plt.xlabel('Number of Births')
plt.show()
The boxplot produced by the Python code above looks like the image below. Notice the
minimum daily count (just below 6000), the maximum daily count (just above 16000), and
the median (approximately 12500) represented by the orange bar.
Boxplots are especially useful for comparing the distribution of two datasets.
Scatterplot
A scatterplot is useful for investigating the relationship between two quantitative variables.
For example, the following Python code plots a scatterplot (using plt.scatter) of a dataset
showing ice cream sales and temperature.
temperature = [14.2, 16.4, 11.9, 15.2, 18.5, 22.1, 19.4, 25.1, 23.4, 18.1, 22.6, 17.2]
icecreamsales = [215, 325, 185, 332, 406, 522, 412, 614, 544, 421, 445, 408]
plt.scatter(temperature, icecreamsales)
plt.title('Ice cream sales vs Temperature')
plt.xlabel('Temperature (degrees C'))
plt.ylabel('Ice cream sales')
plt.show()
A bit of cartoon fun
You might have seen the xkcd webcomics. They often include graphs as part of the cartoon.
Scientific Paper Graph Quality © xkcd, CC BY-NC 2.5
Well, as a bit of fun, adding the line below in the Python code for any of the plots mentioned
above, before the first line starting with plt, gives a cartoon version of the plot in the style of
xkcd.
plt.xkcd()
For example, the scatterplot above becomes:

The Matplotlib library in Python provides a whole range of different graphical plots. Don’t
forget to add a title and axis labels.
Logarithmic scale
Logarithmic Plots in Python (Matplotlib)

Introduction to Logarithmic Plots


What is a Logarithmic Scale?
A logarithmic scale is a nonlinear scale used when there is a large range of quantities. It is
based on orders of magnitude, rather than a standard linear scale.
 Linear scale: Equal distances represent equal values.
 Log scale: Equal distances represent equal ratios (multiplicative).
Why use a Logarithmic Plot?
Scenario Reason for Log Plot

Exponential growth Easier to analyze rapid increases

Scientific measurements Handle values from 10−1010^{-10}10−10 to 10510^5105

Multiplicative relationships Power laws, frequency responses, etc.


Mathematical Background
If values are plotted on a logarithmic axis, spacing corresponds to powers of a base
(commonly 10):
 Points like 1,10,100,10001, 10, 100, 10001,10,100,1000 are equidistant.
 Logarithmic function:
y=log⁡b(x)y = \log_b(x)y=logb(x)
where bbb is typically 10 or e.

Logarithmic Plot Types in Matplotlib


Matplotlib provides built-in functions to create log scale plots:
1. semilogx()
 Logarithmic X-axis
 Linear Y-axis
python
CopyEdit
plt.semilogx(x, y)
2. semilogy()
 Linear X-axis
 Logarithmic Y-axis
python
CopyEdit
plt.semilogy(x, y)
3. loglog()
 Logarithmic X and Y axes
python
CopyEdit
plt.loglog(x, y)
4. set_xscale() and set_yscale()
 Used for custom base (log base 2, base e)
python
CopyEdit
plt.xscale('log', base=2)
plt.yscale('log', base=10)

Example with Code – Semilog and Loglog


Example 1: semilogy()
python
CopyEdit
import numpy as np
import matplotlib.pyplot as
More Advanced Graphical Output
Advanced graphical outputs, often referred to as data visualizations, go beyond simple charts
and graphs to present complex data in a more insightful and interactive way. They use
sophisticated tools and techniques like interactive dashboards, heatmaps, 3D plots, and real-
time data feeds.
Here's a breakdown of advanced graphical outputs:
 Interactive Dashboards:
These dashboards allow users to explore data, filter, drill down into details, and interact with
visualizations, providing a dynamic and engaging experience.
 Heatmaps:
Heatmaps display values across two axis variables as a grid of colored squares, making it
easy to spot patterns and correlations.
 3D Plots:
3D plots visualize data in three dimensions, allowing for a more comprehensive
understanding of complex relationships.
 Real-time Data Feeds:
These visualizations display data as it is being collected, enabling users to monitor trends and
identify anomalies in real-time.
 Other Advanced Techniques:
This also includes techniques like interactive maps, treemaps, network graphs, and animated
visualizations.
Examples of Advanced Data Visualization Tools:
 Matplotlib:
A widely used Python library for creating static, animated, and interactive visualizations.
 Tableau:
A popular data visualization platform for creating interactive dashboards and reports.
 Power BI:
A cloud-based business intelligence service for data analysis and visualization.
 Python libraries like Seaborn, Plotly, and Bokeh:
These libraries offer more advanced visualization capabilities beyond basic Matplotlib.
Benefits of Advanced Graphical Outputs:
 Improved data understanding:
They help users to quickly identify patterns, trends, and relationships in data.
 Enhanced communication:
They make complex data more accessible and easier to understand for a wider audience.
 Better decision-making:
They provide users with the information they need to make informed decisions.
 Increased engagement:
They make data exploration more engaging and interactive

Two plots on the same Axes with different left and right scales.
Plots with multiple axes
The trick is to use two different Axes that share the same x axis. You can use
separate matplotlib.ticker formatters and locators as desired since the two Axes are
independent.
Such Axes are generated by calling the Axes.twinx method. Likewise, Axes.twiny is
available to generate Axes that share a y axis but have different top and bottom scales.
import matplotlib.pyplot as plt
import numpy as np
# Create some mock data
t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)
fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx() # instantiate a second Axes that shares the same x-axis
color = 'tab:blue'
ax2.set_ylabel('sin', color=color) # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.show()
Key Concepts and Techniques:
 Dual-Axis Charts (Two Y-Axes):
This is a common type where one set of data is plotted on the primary Y-axis, and another is
plotted on a secondary Y-axis on the opposite side of the chart.
 Adding Axes in Matplotlib:
Libraries like Matplotlib provide functions like twinx() to create a second Y-axis that shares
the same x-axis, or twiny() for a second X-axis that shares the same Y-axis.
 Positioning and Visibility:
You can control the visibility and position of the secondary axis (e.g., on the left or right side)
using spines and related functions within the charting library.
 Multiple Axes in a Figure:
Libraries like Matplotlib also allow arranging multiple plots within a figure, each with its
own set of axes, using subplots() or tiledlayout().
Example (Conceptual):
Imagine you want to compare temperature (Celsius, primary Y-axis) and rainfall (mm,
secondary Y-axis) over a period of time (X-axis). You could use a dual-axis chart to display
both datasets on the same graph, allowing for easier comparison of how temperature and
rainfall might correlate.
Benefits of Multiple Axes:
 Visual Comparison:
Allows for a side-by-side comparison of different datasets or data with different scales.
 Enhanced Data Interpretation:
Can reveal trends or relationships that might be missed when data is presented separately.
 Complex Data Visualization:
Provides a way to represent more complex data structures and relationships in a single chart.
Tools and Libraries:
 Excel: Provides a built-in feature to add a secondary Y-axis to charts.
 Matplotlib: A powerful Python library for creating static, interactive, and animated
visualizations.
 Plotly: A free and open-source graphing library for Python and JavaScript, with
capabilities for multiple axes.
 BI Office (Pyramid Analytics): Offers multi-axis charts for data analysis and
reporting.
 xViz (Microsoft AppSource): Provides a Power BI visual for creating multi-axis
charts.

Mathematics and Greek Symbols


Greek letters are extensively used in mathematics to represent constants, variables, functions,
and special mathematical concepts. These symbols are borrowed from the Greek alphabet,
and each has a specific and conventional meaning in various contexts.

Greek Alphabet Table (Most Common in Math)


Greek
Uppercase Lowercase Common Usage
Letter

Alpha Α α Angle, alpha particles, significance level (in statistics)

Beta coefficient (finance), angles, regression


Beta Β β
coefficient

Gamma Γ γ Euler’s gamma function, photon, gamma rays

Change (Δx = change in x), derivative, Dirac delta


Delta Δ δ
function

Epsilon Ε ε A very small quantity (limits, calculus), error margin

Zeta Ζ ζ Riemann zeta function

Eta Η η Efficiency (thermodynamics), viscosity

Theta Θ θ Angle (geometry), temperature differences

Iota Ι ι Rarely used, sometimes for unit vectors

Kappa Κ κ Curvature, dielectric constant

Lambda Λ λ Wavelength (physics), eigenvalues

Mu Μ μ Mean (statistics), micro- (10⁻⁶), coefficient of friction

Nu Ν ν Frequency (physics), neutrino

Xi Ξ ξ Random variables, damping ratio

Omicron Ο ο Rarely used in math/science

Mathematical constant (π ≈ 3.14159), product notation


Pi Π π
(∏)

Rho Ρ ρ Density (physics), correlation coefficient


Greek
Uppercase Lowercase Common Usage
Letter

Sigma Σ σ Summation symbol (∑), standard deviation, stress

Tau Τ τ Torque, time constant (electrical circuits)

Upsilon Υ υ Rare, sometimes used in physics

Phi Φ φ Angle, magnetic/electric potential

Chi Χ χ Chi-squared (χ²) test, electronegativity

Psi Ψ ψ Wave function (quantum mechanics)

Omega Ω ω Ohm (resistance), angular velocity, omega constant

Usage Examples in Mathematics


1. Delta (Δ, δ)
 Δx: Change in variable x
 δ (small delta): A very small number (used in calculus and limits)
2. Epsilon (ε)
 Used in limits and calculus:
o “For every ε > 0, there exists a δ > 0 such that…”
3. Sigma (Σ, σ)
 Σ (uppercase sigma): Summation
o ∑i=1ni=1+2+...+n=n(n+1)2\sum_{i=1}^{n} i = 1 + 2 + ... + n = \frac{n(n+1)}
{2}i=1∑ni=1+2+...+n=2n(n+1)
 σ (lowercase sigma): Standard deviation in statistics
4. Pi (π)
 A mathematical constant:
o π=CircumferenceDiameter≈3.1416\pi = \frac{\text{Circumference}}{\
text{Diameter}} \approx 3.1416π=DiameterCircumference≈3.1416
5. Theta (θ)
 Commonly used to represent angles in trigonometry
o sin⁡(θ),cos⁡(θ),tan⁡(θ)\sin(\theta), \cos(\theta), \tan(\theta)sin(θ),cos(θ),tan(θ)
6. Mu (μ)
 Mean of a population in statistics:
o μ=1N∑i=1Nxi\mu = \frac{1}{N} \sum_{i=1}^N x_iμ=N1i=1∑Nxi
 Also used to denote micro (10⁻⁶), e.g., 1 μm = 0.000001 m

Why Use Greek Symbols?


1. Tradition: Many concepts were first studied by Greek mathematicians and scientists.
2. Compactness: Symbols help express complex ideas concisely.
3. Clarity: Different alphabets help distinguish between different types of variables
(e.g., Latin vs Greek).

Applications in Fields
Field Greek Symbols Used Examples

Physics λ, μ, ω, ρ, θ Wavelength, friction, angular speed

Math π, σ, Σ, ε, δ Area of a circle, summation, limits

Statistics μ, σ, χ² Mean, standard deviation, chi-squared test

Engineering τ, φ, Δ Torque, phase angles, change in quantities

Finance β, α Beta coefficient, alpha returns

The Structure of matplotlib


matplotlib is a powerful Python library for creating static, interactive, and animated
visualizations. It is especially known for its pyplot module, which mimics MATLAB's
plotting style.

Structure of matplotlib
At a high level, matplotlib has a hierarchical structure. The key levels are:
1. Figure
2. Axes
3. Axis
4. Artist
Let’s explore each level.

Figure
 The entire canvas or window in which all plots appear.
 It can contain multiple subplots (Axes).
 Think of it as a blank sheet of paper.
python
CopyEdit
import matplotlib.pyplot as plt

fig = plt.figure()
Or using pyplot shortcut:
python
CopyEdit
fig, ax = plt.subplots()

Axes
 A single plot or graph inside the figure.
 A figure can have multiple Axes (subplots).
 An Axes contains:
o X and Y axis (and optionally Z in 3D)
o Labels, titles, lines, ticks, legends, etc.
python
CopyEdit
ax = fig.add_subplot(1, 1, 1)
Or with subplots:
python
CopyEdit
fig, ax = plt.subplots()
Note: Don’t confuse Axes (plot area) with Axis (x-axis or y-axis object).
Axis
 Refers to individual x and y axis objects in an Axes.
 Controls the:
o Scale (linear, log)
o Ticks and tick labels
o Limits (like xlim, ylim)
python
CopyEdit
ax.set_xlim(0, 10)
ax.set_ylim(0, 100)

Artist
 Everything that you see in a plot (lines, text, ticks, labels, etc.) is an Artist.
 Two types:
o Primitive Artists: Lines, text, patches, etc.
o Container Artists: Axes, Figures that contain other artists.
For example:
python
CopyEdit
line = ax.plot(x, y) # line is an Artist
ax.set_title("Title") # Title is an Artist

Complete Hierarchy Diagram


css
CopyEdit
Figure

├──> Axes (one or more)
│ ├──> XAxis, YAxis
│ ├──> Title
│ ├──> Grid, Ticks
│ ├──> Lines, Patches, Text
│ └──> Legend

└──> Other Artists (like suptitle, figure text, etc.)

Example: Simple Plot and Structure


python
CopyEdit
import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

# Create Figure and Axes


fig, ax = plt.subplots()

# Plot
ax.plot(x, y)

# Customize
ax.set_title('Simple Plot')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')

# Show
plt.show()
Additional Components
pyplot Module
 matplotlib.pyplot is a state-based interface to matplotlib, like MATLAB.
 Maintains an implicit figure and axes.
Backends
 matplotlib can render on different backends:
o GUI (TkAgg, Qt5Agg)
o File (PDF, SVG, PNG)

Summary Table
Component Role Example

Figure Entire window/canvas fig = plt.figure()

Axes Single plot inside figure ax = fig.add_subplot(1, 1, 1)

Axis Controls x/y scale, ticks ax.set_xlim()

Artist Any visible element ax.plot(), ax.set_title()

pyplot High-level API plt.plot(), plt.show()

Contour and Vector Field Plots


These types of plots are used to visualize functions of two variables or vector fields:
Plot Type Purpose

Shows level curves of a 3D surface on a 2D plane. Think of it like a


Contour Plot
topographic map.

Vector Field Represents direction and magnitude of vectors over a grid (e.g., wind,
Plot magnetic field).

1. Contour Plots
Definition:
A contour plot shows lines (contours) where a function of two variables f(x, y) is constant.
Each contour line represents a specific height (z-value).
Example: Contour Plot in matplotlib
python
CopyEdit
import numpy as np
import matplotlib.pyplot as plt

# Define function
def f(x, y):
return np.sin(x) ** 2 + np.cos(y) ** 2

# Generate grid
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)

# Create contour plot


plt.contour(X, Y, Z, levels=10, cmap='viridis')
plt.colorbar(label='f(x, y)')
plt.title('Contour Plot of f(x, y)')
plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True)
plt.show()
Key Functions:
 plt.contour() – for basic contour lines
 plt.contourf() – for filled contours
 plt.colorbar() – to show a legend for contour levels

2. Vector Field Plots (Quiver Plots)


Definition:
A vector field plot shows vectors (arrows) at various positions to indicate direction and
magnitude of a vector function F(x, y).
Used for:
 Fluid flow
 Wind direction
 Gradient visualization
Example: Vector Field (Quiver) Plot
python
CopyEdit
import numpy as np
import matplotlib.pyplot as plt

# Define grid
x = np.linspace(-2, 2, 20)
y = np.linspace(-2, 2, 20)
X, Y = np.meshgrid(x, y)

# Define vector components (example: circular field)


U = -Y # X component
V = X # Y component

# Plot vector field


plt.quiver(X, Y, U, V, color='blue')
plt.title('Vector Field Plot')
plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True)
plt.axis('equal')
plt.show()
🛠 Key Functions:
 plt.quiver(X, Y, U, V) – Draws arrows from (X, Y) with components (U, V)
 scale, color, angles – Customize the appearance of arrows

When to Use Which?


Plot Type Use Case Example

Contour Plot Visualize height or potential energy surface

Vector Field Plot Visualize force fields, flow fields, gradients

Summary Table
Function Description

plt.contour() Draws contour lines for f(x, y)

plt.contourf() Draws filled contour regions

plt.quiver() Draws vector field using arrows

plt.colorbar() Adds color legend (for contours or magnitude)

Os Module Vs. Sys Module In Python


Python provides a lot of libraries to interact with the development environment. If you need
help using the OS and Sys Module in Python, you have landed in the right place. This article
covers a detailed explanation of the OS and Sys Module including their comparison. By the
end of this article, you will be able to easily decide which module suits your Python
application development.
What is the OS Module?
OS stands for the Operating System module in Python that allows the developers to interact
with the operating system. It provides functions to perform tasks like file and directory
manipulation, process management, and more. For example, we can get information about the
operating system, such as the current working directory and the user's home directory. This
makes it suitable for managing the system-level operations.
What is the Sys Module?
On the other hand, the System, abbreviated as the Sys Module helps us to interact with the
Python runtime environment. It allows access to variables used or maintained by the Python
interpreter. It also provides tools such as ‘sys.argv’ for command-line arguments, system-
specific parameters, or exiting scripts. Also, developers can easily manipulate parameters
related to the interpreter's configuration, standard streams, and exit codes. Hence, we can
easily manipulate the various parts of the Python Runtime environment.
Sys. Module Vs OS Module
Below are some of the examples by which we can understand the difference between sys
module and os module in Python:
Accessing Details
The Sys Module can access the command line arguments and the os module can access the
location of the script as shown in the below code.
# PythonCode.py executed with ‘python PythonCode.py arg1 arg2 ‘
import sys
import os

# Accessing command-line arguments using sys.argv


script_name = sys.argv[0]
arguments = sys.argv[1:]
# Getting the absolute path of the script using os.path
script_path = os.path.abspath(script_name)
print("Script Name:", script_name)
print("Arguments:", arguments)
print("Script Path:", script_path)
Output:

Returning Path
The OS Module gives the Python Path within the system and the Sys module gives the
Python Path that is shown below:
import os
import sys
# Getting current working directory using os.getcwd()
current_directory = os.getcwd()
# Creating a new directory using os.mkdir()
new_directory1 = os.path.join(current_directory, 'new_folder')
10
os.mkdir(new_directory1)
# Checking Python path using sys.executable
python_path = sys.executable
print("Current Directory:", current_directory)
print("New Directory Path:", new_directory1)
print("Python Path:", python_path)
Output:

Environment Variables and Command Line Arguments


You can use OS Module to get the Environment Variables and Sys module to access the
Command line arguments. This is illustrated as shown below.
import os
import sys
# Getting value of an environment variable using os.environ.get()
python_path = os.environ.get('PYTHONPATH', 'Not found')
# Setting a new environment variable using os.environ
os.environ['NEW_VAR'] = 'new_value'
# Accessing command-line arguments using sys.argv
script_name = sys.argv[0]
arguments = sys.argv[1:]
print("Python Path:", python_path)
print("Script Name:", script_name)
print("Arguments:", arguments)
print("New Environment Variable:", os.environ['NEW_VAR'])
Output:

Differences between OS and Sys Modules

Criteria OS Module Sys Module

It Manages file and It helps us to access Python


Primary Functionality directory operations. interpreter internals.

It is specific to the system


It supports the cross-
and provides only the
platform compatibility.
Cross-Platform Support system’s information.

It is mainly focused on
It offers extensive file
performing the system-level
manipulation operations
File and Directory Ops interactions.

We can modify the file It cannot modify the file


Security Operation permissions. permissions.

It fetches the system


It provides platform-
information during the
independent path handling.
Platform Independence script execution.

It mainly focuses on the


It is limited in command-
command-line arguments
line operations.
Command-Line Handling for script adaptation.
Criteria OS Module Sys Module

It is not directly involved in Here, it provides functions


Memory Management memory-related tasks. for memory usage analysis.

OS Module is focussed on
Customizing Imports It enables customization of
file and directory operations
Python’s import behavior.
without customization.

It facilitates configuration
It does not focus on the file
and interaction with the
interpreter setup..
Interpreter Configuration Python interpreter.


Introduction to File Input and Output
I/O Streams
A stream is a communication channel that a program has with the outside world. It is used to
transfer data items in succession.
An Input/Output (I/O) Stream represents an input source or an output destination. A stream
can represent many different kinds of sources and destinations, including disk files, devices,
other programs, and memory arrays.
Streams support many different kinds of data, including simple bytes, primitive data types,
localized characters, and objects. Some streams simply pass on data; others manipulate and
transform the data in useful ways.
No matter how they work internally, all streams present the same simple model to programs
that use them: A stream is a sequence of data.
Reading information into a program.
A program uses an input stream to read data from a source, one item at a time:
Writing information from a program.
A program uses an output stream to write data to a destination, one item at time:

The data source and data destination pictured above can be anything that holds, generates, or
consumes data. Obviously this includes disk files, but a source or destination can also another
program, a peripheral device, a network socket, or an array.

The Java IO API


The java.io package contains many classes that your programs can use to read and write data.
Most of the classes implement sequential access streams. The sequential access streams can
be divided into two groups: those that read and write bytes and those that read and write
Unicode characters. Each sequential access stream has a speciality, such as reading from or
writing to a file, filtering data as its read or written, or serializing an object.
Types of Streams
Byte Streams: Byte streams perform input and output of 8-bit bytes. They read and write data
one byte at a time. Using byte streams is the lowest level of I/0, so if you are reading or
writing character data the best approach is to use character streams. Other stream types are
built on top of byte streams.
Character Streams: All character stream classes are descended from Reader and Writer.We
are going to use the Reader and Writer classes that perform file
I/0, FileReader and FileWriter. We will look at two examples of writing programs using
character streams, one that reads and writes one character at a time and one that reads and
writes one line at a time.
For sample input, we'll use the example file xanadu.txt, which contains the following verse:
In Xanadu did Kubla Khan
A stately pleasure-dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.
Example 1: Reading and Writing One Character at a time
Here is an example of a program that copies a file one character at a time. The IOTest class
has a copyCharacters method which creates an instance of FileReader and passes the name of
the file to be read to the FileReader constructor. It then creates an instance of FileWriter and
passes the name of the file to be written to the FileWriter constructor.
It then copies the contents of the xanadu.txt file to the characteroutput.txt file, character by
character.
It uses an int variable, "c", to read to and write from. The int variable holds a character value.
Using an int as the return type allows read() to use -1 to indicate that it has reached the end of
the stream.
Notice that there is a block of code preceded by the keyword try and another block of code
preceded by the keyword finally. This is to ensure that the code in the finally block gets
executed, even if the code within the try block causes an exception to be thrown (e.g. if you
try to open a file for which you don't have permission). Don't worry if you don't understand it
completely; you can copy and paste our examples in to your programs.
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class IOTest {


public static void copyCharacters() throws IOException {
FileReader inputStream = null;
FileWriter outputStream = null;
try {
inputStream = new FileReader("xanadu.txt");
outputStream = new FileWriter("xanadu_output.txt");
int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
Always Close Streams
Closing a stream when it's no longer needed is very important. That is why CopyCharacters
uses a finally block to guarantee that both streams will be closed even if an error occurs. This
practice helps avoid serious resource leaks.
One possible error is that CopyCharacters was unable to open one or both files. When that
happens, the stream variable corresponding to the file never changes from its initial null
value. That's why CopyCharacters makes sure that each stream variable contains an object
reference before invoking close.

Example 2: Reading and Writing one line a a time


Character I/O is usually processed in units longer than single characters. One common unit is
the line: a string of characters with a line terminator at the end. A line terminator can be a
carriage-return/line-feed sequence ("\r\n"), a single carriage-return ("\r"), or a single line-feed
("\n"). Supporting all possible line terminators allows programs to read text files created on
any of the widely used operating systems.
Let's modify the copyCharacters example to use line-oriented I/O. To do this, we have to use
two classes we haven't seen before, BufferedReader and PrintWriter.
The copyLines method example invokes BufferedReader.readLine and PrintWriter.println to
do input and output one line at a time.
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
public static void copyLines() throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;
try {
inputStream =
new BufferedReader(new FileReader("xanadu.txt"));
outputStream =
new PrintWriter(new FileWriter("characteroutput.txt"));
String l;
while ((l = inputStream.readLine()) != null) {
outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
Invoking readLine returns a line of text. This line is then output using PrintWriter's println
method, which appends the line terminator for the current operating system. This might not
be the same line terminator that was used in the input file.
Scanning
The Scanner class is available in Java 5 (JDK 1.5). It provides methods to convert text into
appropriate Java types (integer, float, etc). A scanner splits text into a succession of tokens
(text representations of data values) according to specific rules.
For example, in the String object "ab*cd 12.34 253", "ab*cd" is a String token, "12.34" is a
double token and "253" is an integer token.
Objects of type Scanner are useful for breaking down formatted input into tokens and
translating individual tokens according to their data type.
Selected Constructors:
 public Scanner(String s): Constructs a new Scanner that produces values scanned
from the specified string.
 public Scanner(InputStream source): Constructs a new Scanner that produces values
scanned from the specif
Selected Methods:
 public String nextLine(): Scan the next line of input
 public int nextInt(): Scan the next integer
 public double nextDouble(): Scan the next double
See the Scanner API documentation for more methods.
Example 1
By default, a scanner uses white space to separate tokens. (White space characters include
blanks, tabs, and line terminators. For the full list, refer to the documentation
for Character.isWhitespace.) To see how scanning works, let's look at ScanXan, a program
that reads the individual words in xanadu.txt and prints them out, one per line.
import java.io.*;
import java.util.Scanner;
public class ScanXan {
public static void main(String[] args) throws IOException {
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
while (s.hasNext()) {
System.out.println(s.next());
}
} finally {
if (s != null) {
s.close();
}
}
}
}
Notice that ScanXan invokes Scanner's close method when it is done with the scanner object.
Even though a scanner is not a stream, you need to close it to indicate that you're done with
its underlying stream.
The output of ScanXan looks like this:
In
Xanadu
did
Kubla
Khan
A
stately
pleasure-dome
...
To use a different token separator, invoke useDelimiter(), specifying a regular expression. For
example, suppose you wanted the token separator to be a comma, optionally followed by
white space. You would invoke, s.useDelimiter(",\\s*");
Example 2: Translating Individual Tokens
The ScanXan example treats all input tokens as simple String values. Scanner also supports
tokens for all of the Java language's primitive types (except for char), as well as BigInteger
and BigDecimal. Also, numeric values can use thousands separators. Thus, Scanner correctly
reads the string "32,767" as representing an integer value.
The ScanSum example reads a list of double values and adds them up.
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Locale;

public class ScanSum {


public static void main(String[] args) throws IOException {
Scanner s = null;
double sum = 0;
try {
s = new Scanner(
new BufferedReader(new FileReader("usnumbers.txt")));
while (s.hasNext()) {
if (s.hasNextDouble()) {
sum += s.nextDouble();
} else {
s.next();
}
}
} finally {
s.close();
}
System.out.println(sum);
}
}
And here's the sample input file, usnumbers.txt,
8.5
832,767
83.14159
81,000,000.1
The output string is "1032778.74159".

Reading and Writing to text files in Python


Python provides built-in functions for creating, writing, and reading files. Two types of files
can be handled in Python, normal text files and binary files (written in binary language, 0s,
and 1s).
 Text files: In this type of file, Each line of text is terminated with a special character
called EOL (End of Line), which is the new line character ('\n') in Python by default.
 Binary files: In this type of file, there is no terminator for a line, and the data is stored
after converting it into machine-understandable binary language.
This article will focus on opening, closing, reading, and writing data in a text file. Here, we
will also see how to get Python output in a text file.
Table of Content
 Opening a Text File
 Read Text File
 Write to Text File
 Appending to a File
 Closing a Text File
Opening a Text File in Python
It is done using the open() function. No module is required to be imported for this function.
File_object = open(r"File_Name","Access_Mode")
Example: Here, file1 is created as an object for MyFile1 and file2 as object for MyFile2.
# Open function to open the file "MyFile1.txt"
# (same directory) in append mode and
file1 = open("MyFile1.txt","a")
# store its reference in the variable file1
# and "MyFile2.txt" in D:\Text in file2
file2 = open(r"D:\Text\MyFile2.txt","w+")
Also Read: File Mode in Python
Python Read Text File
There are three ways to read txt file in Python:
 Using read()
 Using readline()
 Using readlines()
Reading From a File Using read()
read(): Returns the read bytes in form of a string. Reads n bytes, if no n specified, reads the
entire file.
File_object.read([n])
Reading a Text File Using readline()
readline(): Reads a line of the file and returns in form of a string.For specified n, reads at
most n bytes. However, does not reads more than one line, even if n exceeds the length of the
line.
File_object.readline([n])
Reading a File Using readlines()
readlines(): Reads all the lines and return them as each line a string element in a list.
File_object.readlines()
Note: '\n' is treated as a special character of two bytes.
In this example, a file named "myfile.txt" is created and opened in write mode ( "w" ). Data is
written to the file using write and writelines methods. The file is then reopened in read and
append mode ( "r+" ). Various read operations, including read , readline , readlines , and the
use of seek , demonstrate different ways to retrieve data from the file. Finally, the file is
closed.
file1 = open("myfile.txt", "w")
L = ["This is Delhi \n", "This is Paris \n", "This is London \n"]
# \n is placed to indicate EOL (End of Line)
file1.write("Hello \n")
file1.writelines(L)
file1.close() # to change file access modes
file1 = open("myfile.txt", "r+")
print("Output of Read function is ")
print(file1.read())
print()
# seek(n) takes the file handle to the nth
# byte from the beginning.
file1.seek(0)
print("Output of Readline function is ")
print(file1.readline())
print()

file1.seek(0)
# To show difference between read and readline
print("Output of Read(9) function is ")
print(file1.read(9))
print()
file1.seek(0)
print("Output of Readline(9) function is ")
print(file1.readline(9))
file1.seek(0)
# readlines function
print("Output of Readlines function is ")
print(file1.readlines())
print()
file1.close()
Output:
Output of Read function is
Hello
This is Delhi
This is Paris
This is London
Output of Readline function is
Hello
Output of Read(9) function is
Hello
Th
Output of Readline(9) function is
Hello
Output of Readlines function is
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']

Write to Text File in Python


There are two ways to write in a file:
 Using write()
 Using writelines()
Reference: write() VS writelines()
Writing to a Python Text File Using write()
write(): Inserts the string str1 in a single line in the text file.
File_object.write(str1)
file = open("Employees.txt", "w")
for i in range(3):
name = input("Enter the name of the employee: ")
file.write(name)
file.write("\n")
file.close()
print("Data is written into the file.")
Output:
Data is written into the file.
Writing to a Text File Using writelines()
writelines(): For a list of string elements, each string is inserted in the text file.Used to insert
multiple strings at a single time.
File_object.writelines(L) for L = [str1, str2, str3]
file1 = open("Employees.txt", "w")
lst = []
for i in range(3):
name = input("Enter the name of the employee: ")
lst.append(name + '\n')

file1.writelines(lst)
file1.close()
print("Data is written into the file.")

Output:
Data is written into the file.
Appending to a File in Python
In this example, a file named "myfile.txt" is initially opened in write mode ( "w" ) to write
lines of text. The file is then reopened in append mode ( "a" ), and "Today" is added to the
existing content. The output after appending is displayed using readlines . Subsequently, the
file is reopened in write mode, overwriting the content with "Tomorrow". The final output
after writing is displayed using readlines.
file1 = open("myfile.txt", "w")
L = ["This is Delhi \n", "This is Paris \n", "This is London \n"]
file1.writelines(L)
file1.close()

# Append-adds at last
file1 = open("myfile.txt", "a") # append mode
file1.write("Today \n")
file1.close()

file1 = open("myfile.txt", "r")


print("Output of Readlines after appending")
print(file1.readlines())
print()
file1.close()

# Write-Overwrites
file1 = open("myfile.txt", "w") # write mode
file1.write("Tomorrow \n")
file1.close()

file1 = open("myfile.txt", "r")


print("Output of Readlines after writing")
print(file1.readlines())
print()
file1.close()
Output:
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']
Output of Readlines after writing
['Tomorrow \n']
Working with csv files in Python
Python is one of the important fields for data scientists and many programmers to handle a
variety of data. CSV (Comma-Separated Values) is one of the prevalent and accessible file
formats for storing and exchanging tabular data.
In article explains What is CSV. Working with CSV files in Python, Reading, and Writing to
a CSV file, and Storing Emails in CSV files .
Table of Content
 What is a CSV File?
 Working with CSV files in Python
 Reading a CSV file
 Reading CSV Files Into a Dictionary With csv
 Writing to a CSV file
 Writing a dictionary to a CSV file
 Reading CSV Files With Pandas
 Writing CSV Files with Pandas
 Storing Emails in CSV files

What is a CSV File?


CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a
spreadsheet or database. A CSV file stores tabular data (numbers and text) in plain text. Each
line of the file is a data record. Each record consists of one or more fields, separated by
commas. The use of the comma as a field separator is the source of the name for this file
format. For working CSV files in Python, there is an inbuilt module called CSV.
Working with CSV files in Python
Below are some operations that we perform while working with Python CSV files in Python
 Reading a CSV file
 Reading CSV Files Into a Dictionary With csv
 Writing to a CSV file
 Writing a dictionary to a CSV file
 Reading CSV Files With P andas
 Writing CSV Files With P andas
 Storing email in CSV file

In this article we have covered python function that can used on CSV files but if you wish to
learn python to advance level and having good grip on the python concepts then you should
checkout our Complete python course
Reading a CSV file
Reading from a CSV file is done using the reader object. The CSV file is opened as a text file
with Python’s built-in open() function, which returns a file object. In this example, we first
open the CSV file in READ mode, file object is converted to csv.reader object and further
operation takes place. Code and detailed explanation is given below.
# importing csv module
import csv
# csv file name
filename = "aapl.csv"
# initializing the titles and rows list
fields = []
rows = []
# reading csv file
with open(filename, 'r') as csvfile:
# creating a csv reader object
csvreader = csv.reader(csvfile)
# extracting field names through first row
fields = next(csvreader)
# extracting each data row one by one
for row in csvreader:
rows.append(row)
# get total number of rows
print("Total no. of rows: %d" % (csvreader.line_num))
# printing the field names
print('Field names are:' + ', '.join(field for field in fields))

# printing first 5 rows


print('\nFirst 5 rows are:\n')
for row in rows[:5]:
# parsing each column of a row
for col in row:
print("%10s" % col, end=" "),
print('\n')
Output:

The above example uses a CSV file aapl.csv which can be downloaded from here .
Run this program with the aapl.csv file in the same directory.
 Let us try to understand this piece of code.
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
 Here, we first open the CSV file in READ mode. The file object is named as csvfile .
The file object is converted to csv.reader object. We save the csv.reader object
as csvreader.
fields = csvreader.next()
 csvreader is an iterable object. Hence, .next() method returns the current row and
advances the iterator to the next row. Since, the first row of our csv file contains the
headers (or field names), we save them in a list called fields .
for row in csvreader:
rows.append(row)
 Now, we iterate through the remaining rows using a for loop. Each row is appended to
a list called rows . If you try to print each row, one can find that a row is nothing but a
list containing all the field values.
print("Total no. of rows: %d"%(csvreader.line_num))
 csvreader.line_num is nothing but a counter which returns the number of rows that
have been iterated.
Reading CSV Files Into a Dictionary With csv
We can read a CSV file into a dictionary using the csv module in Python and
the csv.DictReader class. Here's an example:
Suppose, we have a employees.csv file and content inside it will be:
Name ,department ,birthday ,month
John Smith, HR, July
Alice Johnson, IT, October
Bob Williams, Finance, January
In this example, csv.DictReader reads each row of the CSV file as a dictionary where the
keys are the column headers, and the values are the corresponding values in each row. The
dictionaries are then appended to a list ( data_list in this case).
import csv
# Open the CSV file for reading
with open('employees.csv', mode='r') as file:
# Create a CSV reader with DictReader
csv_reader = csv.DictReader(file)
# Initialize an empty list to store the dictionaries
data_list = []
# Iterate through each row in the CSV file
for row in csv_reader:
# Append each row (as a dictionary) to the list
data_list.append(row)
# Print the list of dictionaries
for data in data_list:
print(data)
Output:
{'name': 'John Smith', 'department': 'HR', 'birthday_month': 'July'}
{'name': 'Alice Johnson', 'department': 'IT', 'birthday_month': 'October'}
{'name': 'Bob Williams', 'department': 'Finance', 'birthday_month': 'January'}

Writing to a CSV file


To write to a CSV file, we first open the CSV file in WRITE mode. The file object is
converted to csv.writer object and further operations takes place. Code and detailed
explanation is given below.
# importing the csv module
import csv
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']
# data rows of csv file
rows = [['Nikhil', 'COE', '2', '9.0'],
['Sanchit', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]
# name of csv file
filename = "university_records.csv"
# writing to csv file
with open(filename, 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(rows)
Let us try to understand the above code in pieces.
 fields and rows have been already defined. fields is a list containing all the field
names. rows is a list of lists. Each row is a list containing the field values of that row.
with open(filename, 'w') as csvfile:
csvwriter = csv.writer(csvfile)
 Here, we first open the CSV file in WRITE mode. The file object is named as csvfile .
The file object is converted to csv.writer object. We save the csv.writer object
as csvwriter .
csvwriter.writerow(fields)
 Now we use writerow method to write the first row which is nothing but the field
names.
csvwriter.writerows(rows)
 We use writerows method to write multiple rows at once.
Writing a dictionary to a CSV file
To write a dictionary to a CSV file, the file object (csvfile) is converted to a DictWriter
object. Detailed example with explanation and code is given below.
# importing the csv module
import csv

# my data rows as dictionary objects


mydict = [{'branch': 'COE', 'cgpa': '9.0',
'name': 'Nikhil', 'year': '2'},
{'branch': 'COE', 'cgpa': '9.1',
'name': 'Sanchit', 'year': '2'},
{'branch': 'IT', 'cgpa': '9.3',
'name': 'Aditya', 'year': '2'},
{'branch': 'SE', 'cgpa': '9.5',
'name': 'Sagar', 'year': '1'},
{'branch': 'MCE', 'cgpa': '7.8',
'name': 'Prateek', 'year': '3'},
{'branch': 'EP', 'cgpa': '9.1',
'name': 'Sahil', 'year': '2'}]
# field names
fields = ['name', 'branch', 'year', 'cgpa']
# name of csv file
filename = "university_records.csv"
# writing to csv file
with open(filename, 'w') as csvfile:
# creating a csv dict writer object
writer = csv.DictWriter(csvfile, fieldnames=fields)

# writing headers (field names)


writer.writeheader()
# writing data rows
writer.writerows(mydict)
In this example, we write a dictionary mydict to a CSV file.
with open(filename, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames = fields)
 Here, the file object ( csvfile ) is converted to a DictWriter object. Here, we specify
the fieldnames as an argument.
writer.writeheader()
 writeheader method simply writes the first row of your csv file using the pre-specified
fieldnames.
writer.writerows(mydict)
 writerows method simply writes all the rows but in each row, it writes only the
values(not keys).
So, in the end, our CSV file looks like this:
csv file
Consider that a CSV file looks like this in plain text:

university record
 We notice that the delimiter is not a comma but a semi-colon. Also, the rows are
separated by two newlines instead of one. In such cases, we can specify the delimiter
and line terminator.
Reading CSV Files With P andas
We can read a Python CSV files with Pandas using the pandas.read_csv( ) function. Here's
an example:
Suppose, we have a employees.csv file and content inside it will be:
name,department,birthday_month
John Smith,HR,July
Alice Johnson,IT,October
Bob Williams,Finance,January
In this example, pd.read_csv() reads the CSV file into a Pandas DataFrame. The resulting
DataFrame can be used for various data manipulation and analysis tasks.
import pandas as pd

# Read the CSV file into a DataFrame


df = pd.read_csv('employees.csv')
# Display the DataFrame
print(df)
Output:
name department birthday_month
0 John Smith HR July
1 Alice Johnson IT October
2 Bob Williams Finance January
We can access specific columns, filter data, and perform various operations using pandas
DataFrame functionality. For example, if we want to access the "name" column, we can
use df['name'].
# Access the 'name' column
names = df['name']
print(names)
Output :
0 John Smith
1 Alice Johnson
2 Bob Williams
Name: name, dtype: object
Writing CSV Files with Pandas
We can use Pandas to write CSV files. It can done by using pd.DataFrame() function. In this
example, the Pandas library is used to convert a list of dictionaries ( mydict ) into a
DataFrame, representing tabular data. The DataFrame is then written to a Python CSV file
named "output.csv" using the to_csv method, creating a structured and readable data file for
further analysis or sharing.
import pandas as pd
mydict = [
{'branch': 'COE', 'cgpa': '9.0', 'name': 'Nikhil', 'year': '2'},
{'branch': 'COE', 'cgpa': '9.1', 'name': 'Sanchit', 'year': '2'},
{'branch': 'IT', 'cgpa': '9.3', 'name': 'Aditya', 'year': '2'},
{'branch': 'SE', 'cgpa': '9.5', 'name': 'Sagar', 'year': '1'},
{'branch': 'MCE', 'cgpa': '7.8', 'name': 'Prateek', 'year': '3'},
{'branch': 'EP', 'cgpa': '9.1', 'name': 'Sahil', 'year': '2'}
]
# Create a DataFrame from the list of dictionaries
df = pd.DataFrame(mydict)
# Write the DataFrame to a CSV file
df.to_csv('output.csv', index=False)
Output CSV File:
branch,cgpa,name,year
COE,9.0,Nikhil,2
COE,9.1,Sanchit,2
IT,9.3,Aditya,2
SE,9.5,Sagar,1
MCE,7.8,Prateek,3
EP,9.1,Sahil,2

Storing Emails in CSV files


Here we are importing the csv module and then simply using the same concept of storing the
emails in the form of comma-separated entity also with their names. We're opening the file
open() function and specifying that we need that as a csv file and then writing the each
column into the csv file using writer object.
# importing the csv module
import csv
# field names
fields = ['Name', 'Email']
# data rows of csv file
rows = [ ['Nikhil', '[email protected]'],
['Sanchit', '[email protected]'],
['Aditya', '[email protected]'],
['Sagar', '[email protected]'],
['Prateek', '[email protected]'],
['Sahil', '[email protected]']]
# name of csv file
filename = "email_records.csv"
# writing to csv file
with open(filename, 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(rows)

Output:

You might also like