2 marks Question and Answer
1. What is a Series in Pandas? Write the syntax to create a Series.
Answer:-
A Series in Pandas is a one-dimensional labeled array that can store data of any type such as integers, strings, floats, etc. Each value in a Series is associated with an index.
Syntax:
import pandas as pd
series_name = pd.Series(data)
Example:
import pandas as pd
s = pd.Series([10, 20, 30, 40])
print(s)
Output:
0 10
1 20
2 30
3 40
dtype: int64
2. Differentiate between Series and DataFrame.
Answer :-
| Series | DataFrame |
|---|---|
| A Series is a one-dimensional labeled array. | A DataFrame is a two-dimensional labeled data structure. |
| It contains a single column of data. | It contains multiple rows and columns. |
Created using pd.Series(). |
Created using pd.DataFrame(). |
| Can store data of a single type or mixed types. | Each column can have different data types. |
| Has only one axis (index). | Has two axes (rows and columns). |
Example of Series:
import pandas as pd
s = pd.Series([10, 20, 30])
print(s)
Output:
0 10
1 20
2 30
dtype: int64
Example of DataFrame:
import pandas as pd
df = pd.DataFrame({
'Name': ['Amit', 'Riya'],
'Marks': [85, 90]
})
print(df)
Output:
Name Marks
0 Amit 85
1 Riya 90
3. Write any two advantages of Pandas.
Answer :-
-
Efficient Data Handling:
Pandas provides powerful data structures like
SeriesandDataFrameto store, organize, and manipulate large amounts of data efficiently. - Data Analysis and Cleaning: Pandas offers built-in functions for filtering, sorting, handling missing values, merging datasets, and performing statistical analysis, making data preparation easy.
4. What is a DataFrame? Give an example.
Answer :-
A DataFrame is a two-dimensional data structure in Pandas that stores data in the form of rows and columns, similar to a spreadsheet or a database table. Each column can contain different types of data such as integers, strings, or floats.
Example:
import pandas as pd
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78]
})
print(df)
Output:
Name Marks
0 Amit 85
1 Riya 90
2 Karan 78
5. Explain the use of shape and size attributes.
Answer :-
In Pandas, the shape and size attributes are used to obtain information about the dimensions of a DataFrame.
1. shape Attribute
The shape attribute returns a tuple representing the number
of rows and columns in a DataFrame.
Syntax:
df.shape
Example:
import pandas as pd
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78]
})
print(df.shape)
Output:
(3, 2)
Here, 3 represents the number of rows and 2 represents the number of columns.
2. size Attribute
The size attribute returns the total number of elements
in a DataFrame.
Syntax:
df.size
Example:
import pandas as pd
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78]
})
print(df.size)
Output:
6
6. Differentiate between head() and tail() functions in Pandas.
Answer :-
The head() and tail() functions in Pandas are used
to view records from a DataFrame.
| head() | tail() |
|---|---|
| Displays the first few rows of a DataFrame. | Displays the last few rows of a DataFrame. |
| By default, it shows the first 5 rows. | By default, it shows the last 5 rows. |
| Useful for checking the beginning of a dataset. | Useful for checking the end of a dataset. |
Syntax: df.head(n) |
Syntax: df.tail(n) |
Example:
import pandas as pd
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan', 'Neha', 'Rahul', 'Pooja'],
'Marks': [85, 90, 78, 88, 92, 80]
})
print(df.head(3))
print(df.tail(3))
Output:
head(3)
Name Marks
0 Amit 85
1 Riya 90
2 Karan 78
tail(3)
Name Marks
3 Neha 88
4 Rahul 92
5 Pooja 80
7. Write any two functions used to obtain statistical information from a DataFrame.
Answer :-
Pandas provides several functions to obtain statistical information from a DataFrame. Any two of them are:
1. mean()
Returns the average (mean) value of numeric data in a DataFrame column.
Example:
df['Marks'].mean()
2. max()
Returns the maximum value from a DataFrame column.
Example:
df['Marks'].max()
Other Statistical Functions:
min()– Returns the minimum value.sum()– Returns the sum of values.count()– Returns the number of non-null values.median()– Returns the median value.std()– Returns the standard deviation.
8. Explain the use of the info() function in Pandas.
Answer :-
The info() function in Pandas is used to display a concise
summary of a DataFrame. It provides important information such as:
- Number of rows and columns.
- Column names.
- Number of non-null values in each column.
- Data type (
dtype) of each column. - Memory usage of the DataFrame.
Syntax:
df.info()
Example:
import pandas as pd
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78]
})
df.info()
Output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 3 non-null object
1 Marks 3 non-null int64
dtypes: int64(1), object(1)
memory usage: 176 bytes
9. Write the syntax to read a CSV file using Pandas.
Answer :-
In Pandas, the read_csv() function is used to read data from a
CSV (Comma-Separated Values) file and load it into a DataFrame.
Syntax:
import pandas as pd
df = pd.read_csv("filename.csv")
Example:
import pandas as pd
df = pd.read_csv("students.csv")
print(df)
10. What are row labels and column labels in a DataFrame?
Answer :-
In a Pandas DataFrame, row labels and column labels are used to identify rows and columns.
Row Labels (Index):
Row labels are unique identifiers assigned to each row in a DataFrame.
By default, Pandas assigns row labels starting from
0, 1, 2, ....
Column Labels:
Column labels are the names given to the columns of a DataFrame. They help identify the data stored in each column.
Example:
import pandas as pd
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78]
})
print(df)
Output:
Name Marks
0 Amit 85
1 Riya 90
2 Karan 78
In the above DataFrame:
- Row Labels (Index): 0, 1, 2
- Column Labels: Name, Marks
3marks Question and Answer
1. Explain any three DataFrame attributes with examples.
Answer :-
DataFrame attributes provide information about the structure and contents of a DataFrame. Three commonly used DataFrame attributes are:
1. shape
The shape attribute returns the number of rows and columns
in a DataFrame as a tuple (rows, columns).
Example:
import pandas as pd
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78]
})
print(df.shape)
Output:
(3, 2)
2. size
The size attribute returns the total number of elements
in a DataFrame.
Example:
import pandas as pd
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78]
})
print(df.size)
Output:
6
(3 rows × 2 columns = 6 elements)
3. columns
The columns attribute returns the labels (names) of all
columns in the DataFrame.
Example:
import pandas as pd
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78]
})
print(df.columns)
Output:
Index(['Name', 'Marks'], dtype='object')
Summary Table
| Attribute | Purpose |
|---|---|
shape |
Returns the number of rows and columns. |
size |
Returns the total number of elements. |
columns |
Returns the names of all columns. |
2. Write a program to create a Series of five numbers and display its maximum and minimum value.
Answer :-
import pandas as pd
# Creating a Series of five numbers
s = pd.Series([10, 25, 5, 40, 15])
# Displaying the Series
print("Series:")
print(s)
# Displaying maximum and minimum values
print("Maximum Value:", s.max())
print("Minimum Value:", s.min())
Output:
Series:
0 10
1 25
2 5
3 40
4 15
dtype: int64
Maximum Value: 40
Minimum Value: 5
3. Explain the purpose of describe() function with an example.
Answer :-
The describe() function in Pandas is used to generate
summary statistics of numerical data in a DataFrame. It provides useful
statistical information such as:
- Count of values (
count) - Mean (
mean) - Standard Deviation (
std) - Minimum value (
min) - 25%, 50%, and 75% percentile values
- Maximum value (
max)
Syntax:
df.describe()
Example:
import pandas as pd
df = pd.DataFrame({
'Marks': [85, 90, 78, 88, 92]
})
print(df.describe())
Output:
Marks
count 5.000000
mean 86.600000
std 5.595534
min 78.000000
25% 85.000000
50% 88.000000
75% 90.000000
max 92.000000
Q4. Create a DataFrame containing Name and Marks of three students and display the first two records.
Answer :-
import pandas as pd
# Creating a DataFrame
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78]
})
# Displaying the first two records
print(df.head(2))
Output:
Name Marks
0 Amit 85
1 Riya 90
Q5. Differentiate between Series and DataFrame with suitable examples.
Answer :-
Series and DataFrame are the two main data structures provided by the Pandas library. A Series is a one-dimensional labeled array, whereas a DataFrame is a two-dimensional labeled data structure that consists of rows and columns.
Difference between Series and DataFrame
| Series | DataFrame |
|---|---|
| A Series is a one-dimensional labeled array. | A DataFrame is a two-dimensional labeled data structure. |
| It contains a single column of data. | It contains multiple rows and columns. |
Created using pd.Series(). |
Created using pd.DataFrame(). |
| Has only one axis (Index). | Has two axes (Rows and Columns). |
| Suitable for storing a single list of values. | Suitable for storing tabular data. |
| Can store one column of data. | Can store multiple columns with different data types. |
| Uses index labels only. | Uses both row labels (index) and column labels. |
Example of Series
import pandas as pd
s = pd.Series([10, 20, 30, 40])
print(s)
Output
0 10
1 20
2 30
3 40
dtype: int64
Example of DataFrame
import pandas as pd
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78]
})
print(df)
Output
Name Marks
0 Amit 85
1 Riya 90
2 Karan 78
Conclusion
A Series is used to store a single column of labeled data, while a DataFrame is used to store data in a tabular form consisting of multiple rows and columns. DataFrames are more flexible and are widely used for data analysis and data manipulation in Pandas.
Q6. Write a program to create a DataFrame and display its shape, size, and dimensions.
Answer :-
In Pandas, a DataFrame is a two-dimensional data structure
used to store data in rows and columns. The attributes
shape, size, and ndim provide
important information about the DataFrame.
- shape – Returns the number of rows and columns as a tuple
(rows, columns). - size – Returns the total number of elements in the DataFrame.
- ndim – Returns the number of dimensions of the DataFrame. For a DataFrame, the value is always 2.
Program:
import pandas as pd
# Creating a DataFrame
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78]
})
# Displaying the DataFrame
print("DataFrame:")
print(df)
# Displaying shape
print("\nShape:", df.shape)
# Displaying size
print("Size:", df.size)
# Displaying dimensions
print("Dimensions:", df.ndim)
Output:
DataFrame:
Name Marks
0 Amit 85
1 Riya 90
2 Karan 78
Shape: (3, 2)
Size: 6
Dimensions: 2
Explanation:
| Attribute | Output | Description |
|---|---|---|
shape |
(3, 2) | Shows that the DataFrame has 3 rows and 2 columns. |
size |
6 | Shows the total number of elements (3 × 2 = 6). |
ndim |
2 | Indicates that a DataFrame is two-dimensional. |
Conclusion:
The shape, size, and ndim attributes
help users understand the structure and dimensions of a DataFrame.
These attributes are frequently used in data analysis to inspect datasets
before performing further operations.
Q7. Explain the functions count(), max(), and min() with examples.
Answer :-
Pandas provides several built-in functions to perform statistical operations
on the data stored in a DataFrame or Series. The
count(), max(), and min() functions
are commonly used to analyze numerical data.
1. count() Function
The count() function returns the number of non-null
(non-missing) values in a DataFrame or Series.
Syntax:
df['Column_Name'].count()
Example:
import pandas as pd
df = pd.DataFrame({
'Marks': [85, 90, 78, 88, 92]
})
print(df['Marks'].count())
Output:
5
2. max() Function
The max() function returns the largest value
from a DataFrame column or Series.
Syntax:
df['Column_Name'].max()
Example:
import pandas as pd
df = pd.DataFrame({
'Marks': [85, 90, 78, 88, 92]
})
print(df['Marks'].max())
Output:
92
3. min() Function
The min() function returns the smallest value
from a DataFrame column or Series.
Syntax:
df['Column_Name'].min()
Example:
import pandas as pd
df = pd.DataFrame({
'Marks': [85, 90, 78, 88, 92]
})
print(df['Marks'].min())
Output:
78
Summary Table
| Function | Purpose | Example Output |
|---|---|---|
count() |
Returns the number of non-null values. | 5 |
max() |
Returns the largest value. | 92 |
min() |
Returns the smallest value. | 78 |
Conclusion
The count(), max(), and min()
functions are essential statistical functions in Pandas. They help users
quickly analyze data by finding the number of available values, the
highest value, and the lowest value in a DataFrame or Series.
Q8. Write a program to create a Series with custom indexes.
Answer :-
In Pandas, a Series is a one-dimensional labeled array.
By default, Pandas assigns index values starting from 0.
However, we can also create a Series with custom indexes
using the index parameter of the pd.Series() function.
Syntax:
series_name = pd.Series(data, index=index_values)
Program:
import pandas as pd
# Creating a Series with custom indexes
s = pd.Series(
[85, 90, 78],
index=['Amit', 'Riya', 'Karan']
)
# Displaying the Series
print(s)
Output:
Amit 85
Riya 90
Karan 78
dtype: int64
Explanation:
- The
pd.Series()function is used to create a Series. - The
indexparameter is used to assign custom labels instead of the default numeric indexes. - Here, Amit, Riya, and Karan are the custom index labels.
- The corresponding values are 85, 90, and 78.
Summary Table
| Function | Purpose |
|---|---|
pd.Series() |
Creates a Series object. |
index |
Assigns custom labels to the Series. |
Conclusion:
Custom indexes make the data more meaningful and easier to access. Instead of using default numeric indexes, descriptive labels can be assigned to identify each value in the Series.
Q9. What is the purpose of dtypes and columns attributes? Explain with example.
Answer :-
In Pandas, dtypes and columns are important DataFrame
attributes that provide information about the structure of the DataFrame.
The dtypes attribute is used to display the data type of each
column, while the columns attribute is used to display the names
of all the columns present in the DataFrame.
1. dtypes Attribute
The dtypes attribute returns the data type (dtype)
of every column in the DataFrame. It helps identify whether a column contains
integers, floating-point numbers, strings, boolean values, etc.
Syntax:
df.dtypes
2. columns Attribute
The columns attribute returns the names (labels) of all columns
in the DataFrame. It is useful when we want to know or access the available
columns in a dataset.
Syntax:
df.columns
Example Program
import pandas as pd
# Creating a DataFrame
df = pd.DataFrame({
'Name': ['Amit', 'Riya', 'Karan'],
'Marks': [85, 90, 78],
'Passed': [True, True, False]
})
# Displaying data types
print("Data Types:")
print(df.dtypes)
# Displaying column names
print("\nColumn Names:")
print(df.columns)
Output
Data Types:
Name object
Marks int64
Passed bool
dtype: object
Column Names:
Index(['Name', 'Marks', 'Passed'], dtype='object')
Explanation
- Name has the data type
objectbecause it stores text values. - Marks has the data type
int64because it stores integer values. - Passed has the data type
boolbecause it stores Boolean values (TrueorFalse). - The
columnsattribute returns the names of all columns: Name, Marks, and Passed.
Summary Table
| Attribute | Purpose | Example Output |
|---|---|---|
dtypes |
Displays the data type of each column. | Name → object, Marks → int64, Passed → bool |
columns |
Displays the names of all columns. | Index(['Name', 'Marks', 'Passed']) |
Conclusion
The dtypes and columns attributes are very useful
while working with DataFrames. They help users understand the data types of
each column and identify all column names, making data analysis and
manipulation easier.
Q10. Write a program to read a CSV file and display the first five records.
Answer :-
A CSV (Comma-Separated Values) file is a text file used to
store tabular data such as student records, employee details, and sales
information. In Pandas, the read_csv() function is used to read
data from a CSV file and store it in a DataFrame. The
head() function is then used to display the first few records
of the DataFrame. By default, head() displays the first
5 rows.
Syntax:
import pandas as pd
df = pd.read_csv("filename.csv")
print(df.head())
Program:
import pandas as pd
# Reading the CSV file
df = pd.read_csv("students.csv")
# Displaying the first five records
print(df.head())
Sample CSV File (students.csv):
Name,Marks,Grade
Amit,85,A
Riya,90,A+
Karan,78,B
Neha,88,A
Rahul,92,A+
Pooja,80,B+
Output:
Name Marks Grade
0 Amit 85 A
1 Riya 90 A+
2 Karan 78 B
3 Neha 88 A
4 Rahul 92 A+
Explanation:
- The
import pandas as pdstatement imports the Pandas library. - The
read_csv()function reads the contents of thestudents.csvfile and stores them in a DataFrame. - The
head()function displays the first five records of the DataFrame. - If the DataFrame contains fewer than five rows, all available rows are displayed.
Summary Table
| Function | Purpose |
|---|---|
read_csv() |
Reads data from a CSV file and creates a DataFrame. |
head() |
Displays the first five records of a DataFrame by default. |
Conclusion:
The read_csv() function is used to import data from a CSV file,
while the head() function is used to quickly view the first
five records of the dataset. These functions are widely used in data
analysis to inspect data before performing further operations.


