0% found this document useful (0 votes)
28 views65 pages

Lectur2 PANDAS

The document contains questions and answers related to pandas library in Python. It discusses that PANDAS stands for Panel Data, pandas is an important library for data analysis, important data structures in pandas are Series and DataFrame, CSV files are read as DataFrame, and matplotlib is used for data visualization. It also contains examples of plotting graphs, bar charts, pie charts and histograms using pandas and matplotlib.

Uploaded by

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

Lectur2 PANDAS

The document contains questions and answers related to pandas library in Python. It discusses that PANDAS stands for Panel Data, pandas is an important library for data analysis, important data structures in pandas are Series and DataFrame, CSV files are read as DataFrame, and matplotlib is used for data visualization. It also contains examples of plotting graphs, bar charts, pie charts and histograms using pandas and matplotlib.

Uploaded by

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

Q.

PANDAS stands for _____________

A. Panel Data Analysis


B. Panel Data analyst
C. Panel Data
D. Panel Dashboard
Answer : C
Q. __________ is an important library used for analyzing data.

A. Math
B. Pandas
C. Random
D. None of the above
Answer : B
Q. Important data structure of pandas is/are ___________

A. Series
B. Data Frame
C. Both of the above
D. None of the above
Answer : C
• Series can only contain single list with index, whereas dataframe can
be made of more than one series or we can say that a dataframe is a
collection of series that can be used to analyse the data.
Q. Which of the following object you get after reading CSV file?

A. DataFrame
B. Character Vector
C. Panel
D. All of the mentioned
Answer : A
Q. Which of the following library in Python is used for plotting
graphs and visualization.

A. Matplotlib
B. Pandas
C. NumPy
D. None of the above
Answer : A
Q. The most important object defined in NumPy is an N-dimensional
array type called?

A. ndarray
B. narray
C. nd_array
D. darray
Answer : A
Draw 2 plots
import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x,y)

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x,y)

plt.show()
Draw 2 plots on top of each other:
import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 1, 1)
plt.plot(x,y)

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 1, 2)
plt.plot(x,y)

plt.show()
Bar chart in python
• A bar chart or bar graph is a chart or graph that presents categorical data
with rectangular bars with heights or lengths proportional to the values that
they represent. The bars can be plotted vertically or horizontally.
• A bar graph shows comparisons among discrete categories. One axis of the
chart shows the specific categories being compared, and the other axis
represents a measured value.
• Matplotlib API provides the bar() function that can be used in the MATLAB
style use as well as object oriented API. The signature of bar() function to be
used with axes object is as follows −
• ax.bar(x, height, width, bottom, align)The function makes a bar plot with the
bound rectangle of size (x −width = 2; x + width=2; bottom; bottom + height).
The parameters to the function are −

x sequence of scalars representing the x coordinates of the bars. align controls if x is


the bar center (default) or left edge.

height scalar or sequence of scalars representing the height(s) of the bars.

width scalar or array-like, optional. the width(s) of the bars default 0.8

bottom scalar or array-like, optional. the y coordinate(s) of the bars default None.

align {‘center’, ‘edge’}, optional, default ‘center’


import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
langs = ['C', 'C++', 'Java', 'Python', 'PHP’]
students = [23,17,35,29,12]
ax.bar(langs,students) plt.show()
Pie Chart
• A pie chart is a type of graph that records data in a circular manner
that is further divided into sectors for representing the data of that
particular part out of the whole part. Each of these sectors or slices
represents the proportionate part of the whole. Pie charts, also
commonly known as pie diagrams help in interpreting and
representing the data more clearly. It is also used to compare the
given data.
• As you can see the pie chart draws one piece (called a wedge) for
each value in the array (in this case [35, 25, 25, 15]).
• By default the plotting of the first wedge starts from the x-axis and
move counterclockwise:
A simple pie chart
• import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])

plt.pie(y)
plt.show()
• import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)


plt.show()
Add legend in pie chart
• import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)


plt.legend()
plt.show()
Histogram in python
• A histogram is a graphical representation that organizes a group of
data points into user-specified ranges. Similar in appearance to a
bar graph, the histogram condenses a data series into an easily
interpreted visual by taking many data points and grouping them into
logical ranges or bins.
import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(170, 10, 250)

plt.hist(x)
plt.show()
from matplotlib import pyplot as plt

x = [300, 400, 500, 2000, 10]

plt.hist(x, 10)

plt.show()

You might also like