0% found this document useful (0 votes)
26 views3 pages

Python Data Science Libraries Tutorial

The document provides a comprehensive overview of Python libraries NumPy, Pandas, and Matplotlib, covering topics such as data types, array operations, data structures, and plotting techniques. It includes code examples for creating and manipulating arrays, handling missing data, and visualizing data through various types of plots. Key concepts like fancy indexing, aggregation functions, and differences between data structures are also explained.

Uploaded by

hhhrrruu053
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)
26 views3 pages

Python Data Science Libraries Tutorial

The document provides a comprehensive overview of Python libraries NumPy, Pandas, and Matplotlib, covering topics such as data types, array operations, data structures, and plotting techniques. It includes code examples for creating and manipulating arrays, handling missing data, and visualizing data through various types of plots. Key concepts like fancy indexing, aggregation functions, and differences between data structures are also explained.

Uploaded by

hhhrrruu053
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

Python - NumPy, Pandas, Matplotlib Q&A (5M & 10M)

1) Explain NumPy datatypes in detail with examples:


NumPy supports int, float, bool, string, complex etc.
Example:
import numpy as np
arr = [Link]([1,2,3], dtype=np.float32)
print(arr, [Link])

2) Write a Python program using NumPy to create an array of 10 integers, and find maximum, minimum, mean and standard
import numpy as np
arr = [Link](1,11)
print('Max:', [Link]())
print('Min:', [Link]())
print('Mean:', [Link]())
print('Std:', [Link]())

3) Explain fancy indexing & sorting in NumPy with examples:


Fancy indexing uses integer arrays/lists to access elements.
arr = [Link]([10,20,30,40,50])
print(arr[[0,3,4]]) # Fancy indexing
print([Link](arr)) # Sorting

4) Explain Pandas Series and DataFrame objects with examples:


Series = 1D labeled array, DataFrame = 2D labeled table.
Example:
import pandas as pd
s = [Link]([1,2,3], index=['a','b','c'])
df = [Link]({'A':[1,2],'B':[3,4]})

5) Write a Pandas program to combine two datasets and perform group-wise aggregation:
import pandas as pd
df1 = [Link]({'ID':[1,2,3],'Value':[10,20,30]})
df2 = [Link]({'ID':[1,2,3],'Category':['X','Y','X']})
merged = [Link](df1, df2, on='ID')
print([Link]('Category')['Value'].mean())

6) Explain different methods of handling missing data in Pandas with examples:


- dropna(): remove missing values
- fillna(value): fill with value
- interpolate(): estimate missing values

7) None vs NaN:
None = Python null object, type NoneType. NaN = Not a Number, float.
In Pandas numeric columns, None becomes NaN. Both detected by isnull().
8) What is NumPy?
Library for numerical computing. Provides fast ndarrays, linear algebra, stats, transforms.

9) Difference between Python lists & NumPy arrays:


List can hold different types, slower. NumPy arrays homogeneous, faster, vectorized ops.

10) Short note on aggregation functions:


Functions that summarize data: [Link], [Link], [Link], [Link], [Link].

11) Write two examples in NumPy:


arr = [Link]([1,2,3])
print([Link](arr))
print([Link](arr))

12) What is Boolean masking?


Filtering arrays with conditions.
arr = [Link]([10,20,30])
print(arr[arr>15])

13) Main data structures in Pandas:


Series (1D), DataFrame (2D), Panel (3D, deprecated).

14) Difference between loc[] and iloc[]:


loc = label-based indexing, iloc = integer index-based.
15) Hierarchical indexing:
Using multiple index levels.
[Link]([1,2,3,4], index=[['A','A','B','B'],['x','y','x','y']])

16) Two methods to handle missing data:


- dropna()
- fillna(value)

17) Python code to plot line chart and scatter plot:


import [Link] as plt
x=[1,2,3]; y=[2,4,1]
[Link](x,y)
[Link](x,y)
[Link]()

18) Explain Histograms and density plots:


Histogram shows frequency distribution. Density plot shows probability distribution.
[Link](data, bins=10, density=True)

19) Four types of plots: Line, Scatter, Bar, Histogram.

20) Use of xlabel() & ylabel():


[Link]('X-axis') sets x-label, [Link]('Y-axis') sets y-label.
Difference Line vs Scatter: Line connects points, Scatter shows separate points.
Histogram: frequency distribution plot.

Common questions

Powered by AI

Boolean masking in NumPy involves creating a boolean array based on conditions applied to data elements, which is then used to filter data. For example, for an array 'arr = np.array([10,20,30])', applying 'arr[arr>15]' would return 'array([20, 30])'. This method allows for efficient and concise data manipulation, facilitating selective data extraction without explicit loops .

Python lists are collections that can store values of different types and have slower performance for numerical operations due to lack of optimization. In contrast, NumPy arrays are homogeneous collections of elements with the same type (usually numerical), optimized for performance with vectorized operations. Developers often choose NumPy arrays for numerical computing because they provide both significant speed advantages and a rich set of mathematical functions .

Hierarchical indexing, or multi-indexing, in Pandas allows users to create a Series or DataFrame with multiple levels of indexing. This enables more complex data relationships and facilitates operations on multiple dimensions, such as slicing, dicing, and group-level aggregation. It is particularly beneficial when dealing with data that naturally forms hierarchies or when storing different dimensions of data compactly .

Pandas provides several methods for handling missing data: - 'dropna()' deletes rows or columns with missing data, useful when missingness is negligible relative to dataset size. - 'fillna(value)' replaces missing data with a specified value, advantageous when specific default values are meaningful or non-disruptive. - 'interpolate()' estimates missing data using interpolation, suitable in time-series data where small gaps exist .

'loc[]' is preferred when the dataset involves labeled indexing, which allows for more intuitive data access using labels. It is particularly useful when dealing with datasets that have meaningful categorical labels for rows or columns. On the other hand, 'iloc[]' uses purely integer-based indexing, which is advantageous when dealing with numerical data manipulations. Choosing 'loc[]' helps maintain readability and semantic integrity of the code, whereas 'iloc[]' might be needed for efficiency in numerical iterations .

Histograms display frequency distributions, showing the number of data points within each bin. They are ideal for exploring the distribution of discrete data or understanding data spread over intervals. Density plots portray probability distributions, smoothing the dataset into a continuous curve, which is appropriate for visualizing the distribution of continuous data and recognizing underlying patterns without bin-specific artifacts .

In Python, 'None' is used as a null object of type 'NoneType', while 'NaN' (Not a Number) is a float value representing undefined or unrepresentable numerical results. Pandas treats 'None' and 'NaN' similarly in numeric columns, converting 'None' to 'NaN', which can then be handled using methods like 'isnull()'. This conversion ensures consistency in handling missing or undefined values, especially crucial in data frames and series operations .

NumPy is a foundational library essential for numerical computations, providing efficient array handling and mathematical operations. It forms the basis for many operations within Python's data science ecosystem. Pandas builds on top of NumPy, offering high-level data structures like Series and DataFrames, which facilitate handling complex datasets. While NumPy focuses on numerical data, Pandas is more applicable for data manipulation and analysis, capable of handling data with a variety of forms, labels, and contexts .

Fancy indexing in NumPy allows accessing an array using arrays or lists of integers as indices, which can be non-consecutive and repeating, unlike basic indexing that accesses elements by single scalar index values. For example, 'arr = np.array([10,20,30,40,50])' and 'arr[[0,3,4]]' will produce '[10 40 50]'. This method is advantageous for non-sequential or pattern-based data extraction, enhancing flexibility in data manipulation .

Aggregation functions are crucial in data analysis as they synthesize extensive datasets into more comprehensible summary statistics, enabling quick insights into patterns and trends. Examples in NumPy include 'np.sum' for summing elements, 'np.mean' for calculating the mean, 'np.min' and 'np.max' for finding minimum and maximum values, and 'np.std' for computing the standard deviation .

You might also like