Data Handling Using
Data Handling Using — Study Notes
NCERT-aligned · 16 notes · 3 shown free
2.1 INTRODUCTION TO PYTHON LIBRARIES
Explanation2.1 INTRODUCTION TO PYTHON LIBRARIES
Python libraries are collections of built-in modules that facilitate performing numerous actions without the need to write detailed programs. Each library contains multiple modules that can be imported and used as needed. In scientific and analytical computing, three major Python libraries are NumPy, Pandas, and Matplotlib. NumPy (Numerical Python) is used for numerical data analysis and scientific computing, featuring a multidimensional array object that allows fast access to elements stored contiguously in memory. Pandas (Panel Data) is a high-level data manipulation tool built on top of NumPy and Matplotlib, designed for easy data import, export, analysis, and visualization. It provides three key data structures: Series, DataFrame, and Panel, which simplify organized and efficient data analysis. Matplotlib is a plotting library that enables the creation of publication-quality graphs such as histograms, bar charts, and scatterplots with minimal code. It integrates well with NumPy and Pandas. The need for Pandas arises because NumPy arrays require homogeneous data types, while Pandas DataFrames can handle heterogeneous data types (e.g., float, int, string, datetime). Pandas also offers simpler interfaces for file loading, plotting, selection, joining, and group operations, making it ideal for tabular data processing, whereas NumPy is best suited for numeric array-based data manipulation. To install Pandas, Python must be pre-installed, and the command 'pip install pandas' is used.
- Python libraries contain modules for various functionalities.
- NumPy provides multidimensional arrays for numerical computing.
- Pandas offers Series, DataFrame, and Panel data structures for data analysis.
- Matplotlib is used for data visualization and plotting.
- Pandas supports heterogeneous data types; NumPy requires homogeneous data.
- Pandas simplifies data operations like file loading and grouping.
- 📌 Python libraries: Collections of modules for specific tasks.
- 📌 NumPy: Library for numerical computing with arrays.
- 📌 Pandas: Library for data manipulation with Series and DataFrames.
2.1.2 Data Structure in Pandas
Concept2.1.2 Data Structure in Pandas
A data structure is a collection of data values along with operations that can be applied to them, enabling efficient storage, retrieval, and modification. In Pandas, two main data structures are used extensively: Series and DataFrame. A Series is a one-dimensional labeled array capable of holding any data type (integer, float, string, etc.) with an associated index that labels each element. By default, the index is numeric starting from zero but can be customized with labels of any data type. A DataFrame is a two-dimensional labeled data structure with rows and columns, similar to a table in a relational database or a spreadsheet. Each column in a DataFrame can have a different data type, and both row and column indices are labeled. These data structures are designed to make data analysis organized, effective, and efficient. To work with these, the Pandas library must first be imported.
- Data structure stores data values and operations on them.
- Pandas Series is a one-dimensional labeled array.
- Series have an index labeling each element, customizable by the user.
- DataFrame is a two-dimensional labeled data structure with rows and columns.
- Each DataFrame column can have different data types.
- Pandas data structures facilitate organized data analysis.
- 📌 Data structure: Organized collection of data and operations.
- 📌 Series: One-dimensional labeled array in Pandas.
- 📌 DataFrame: Two-dimensional labeled table-like data structure.
2.2 SERIES
Explanation2.2 SERIES
A Pandas Series is a one-dimensional array-like object that holds a sequence of values of any data type, such as integers, floats, strings, or lists. Each element in a Series has an associated label called an index, which by default is a numeric sequ
Practice Questions — Data Handling Using
Includes NCERT exercise questions with answers
Q1.1. What is a Series and how is it different from a 1-D array, a list and a dictionary?
Answer:
A Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). It is a core data structure in the pandas library. Unlike a 1-D array which only has integer-based indexing, a Series has an associated index that labels each element, allowing for more flexible data access. Compared to a list, which is a basic Python data structure without labels, a Series provides labeled indexing and many built-in methods for data manipulation. Unlike a dictionary, which is an unordered collection of key-value pairs, a Series maintains order and supports vectorized operations.
Explanation:
A Series differs from a 1-D array by having labeled indices, from a list by providing labels and vectorized operations, and from a dictionary by maintaining order and supporting array-like operations.
Q2.2. What is a DataFrame and how is it different from a 2-D array?
Answer:
A DataFrame is a two-dimensional labeled data structure with columns of potentially different types. It is similar to a spreadsheet or SQL table. Unlike a 2-D array which can only hold elements of the same data type, a DataFrame can hold heterogeneous data types across columns. Additionally, DataFrames have labeled rows and columns, allowing for more intuitive data access and manipulation.
Explanation:
DataFrames differ from 2-D arrays by supporting heterogeneous data types and labeled axes (rows and columns), making them more versatile for data analysis.
Q3.3. How are DataFrames related to Series?
Answer:
A DataFrame is essentially a collection of Series objects that share the same index. Each column in a DataFrame is a Series. Thus, a DataFrame can be thought of as a dictionary of Series objects, where each Series represents a column.
Explanation:
Understanding that DataFrames are composed of multiple Series helps in manipulating and accessing data efficiently.
Q4.4. What do you understand by the size of (i) a Series, (ii) a DataFrame?
Answer:
The size of a Series is the number of elements it contains. For a DataFrame, the size is the total number of elements, which is the product of the number of rows and columns.
Explanation:
Size helps in understanding the amount of data contained in the Series or DataFrame.
Q5.5. Create the following Series and do the specified operations: a) EngAlph, having 26 elements with the alphabets as values and default index values. b) Vowels, having 5 elements with index labels ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’ and all the five values set to zero. Check if it is an empty series. c) Friends, from a dictionary having roll numbers of five of your friends as data and their first name as keys. d) MTseries, an empty Series. Check if it is an empty series. e) MonthDays, from a numpy array having the number of days in the 12 months of a year. The labels should be the month numbers from 1 to 12.
Answer:
a) EngAlph = pd.Series(list('abcdefghijklmnopqrstuvwxyz')) b) Vowels = pd.Series([0,0,0,0,0], index=['a','e','i','o','u']) Check empty: Vowels.empty # Returns False c) Friends = pd.Series({ 'Alice': 101, 'Bob': 102, 'Charlie': 103, 'David': 104, 'Eva': 105 }) d) MTseries = pd.Series([]) Check empty: MTseries.empty # Returns True Note: Creating empty Series without dtype will raise warning in recent pandas versions, so use pd.Series(dtype='float64') e) import numpy as np MonthDays = pd.Series(np.array([31,28,31,30,31,30,31,31,30,31,30,31]), index=range(1,13))
Explanation:
Each Series is created using pandas.Series with appropriate data and index. Empty Series can be checked using the .empty attribute.
Q6.6. Using the Series created in Question 5, write commands for the following: a) Set all the values of Vowels to 10 and display the Series. b) Divide all values of Vowels by 2 and display the Series. c) Create another series Vowels1 having 5 elements with index labels ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’ having values [2,5,6,3,8] respectively. d) Add Vowels and Vowels1 and assign the result to Vowels3. e) Subtract, Multiply and Divide Vowels by Vowels1. f) Alter the labels of Vowels1 to [‘A’, ‘E’, ‘I’, ‘O’, ‘U’].
Answer:
a) Vowels[:] = 10 print(Vowels) b) Vowels = Vowels / 2 print(Vowels) c) Vowels1 = pd.Series([2,5,6,3,8], index=['a','e','i','o','u']) d) Vowels3 = Vowels + Vowels1 print(Vowels3) e) Subtract: Vowels - Vowels1 Multiply: Vowels * Vowels1 Divide: Vowels / Vowels1 f) Vowels1.index = ['A', 'E', 'I', 'O', 'U'] print(Vowels1)
Explanation:
These operations demonstrate element-wise arithmetic and index manipulation on Series objects.
Q7.7. Using the Series created in Question 5, write commands for the following: a) Find the dimensions, size and values of the Series EngAlph, Vowels, Friends, MTseries, MonthDays. b) Rename the Series MTseries as SeriesEmpty. c) Name the index of the Series MonthDays as monthno and that of Series Friends as Fname. d) Display the 3rd and 2nd value of the Series Friends, in that order. e) Display the alphabets 'e' to 'p' from the Series EngAlph. f) Display the first 10 values in the Series EngAlph. g) Display the last 10 values in the Series EngAlph. h) Display the MTseries.
Answer:
a) For each Series: - Dimensions: .ndim - Size: .size - Values: .values Example: EngAlph.ndim, EngAlph.size, EngAlph.values b) SeriesEmpty = MTseries.rename('SeriesEmpty') c) MonthDays.index.name = 'monthno' Friends.index.name = 'Fname' d) Friends.iloc[[2,1]] # 3rd and 2nd values in that order e) EngAlph['e':'p'] # Slicing from 'e' to 'p' f) EngAlph.head(10) g) EngAlph.tail(10) h) print(MTseries)
Explanation:
These commands demonstrate accessing metadata, renaming, indexing, slicing, and displaying Series data.
Q8.8. Using the Series created in Question 5, write commands for the following: a) Display the names of the months 3 through 7 from the Series MonthDays. b) Display the Series MonthDays in reverse order.
Answer:
a) MonthDays.loc[3:7] b) MonthDays[::-1]
Explanation:
Using .loc for label-based slicing and Python slicing syntax for reversing the Series.
All 7 Chapters in Informatics Practices
Informatics Practices · Class 12