Sunday 24 March 2024

Pandas DataFrame MCQs with Answers

 

Python Programming MCQs

Pandas DataFrame


10 important MCQs related to Python Pandas DataFrame, along with their answers:


1. Which of the following statements accurately describes a DataFrame?

a) A one-dimensional labeled array-like object

b) A multi-dimensional labeled data structure with potentially different data types in each column

c) A collection of Series objects with the same index

d) A specialized dictionary for storing numerical data

2. How do you create a DataFrame from a Python dictionary?

a) df = pd.Series(data)

b) df = pd.DataFrame(dict, index=index_list)

c) df = pd.Array.from_dict(data)

d) df = pd.Dict(data)

3. To access a specific column in a DataFrame, you would use:

a) df.get('column_name')

b) df['column_name']

c) df.column_name

d) df.at['column_name']

4. How can you filter rows in a DataFrame based on a condition?

a) df.filter(condition)

b) df.where(condition)

c) df[condition]

d) df.query(condition)

5. What function would you use to display the summary statistics of a DataFrame's numerical columns?

a) df.head()

b) df.info()

c) df.describe()

d) df.summary()

6. Which function removes missing values (NaN) from a DataFrame?

a) df.fillna(value)

b) df.replace(NaN, value)

c) df.dropna()

d) df.clean()

7. How can you replace missing values (NaN) with a specific value?

a) df.fillna(value)

b) df.replace(NaN, value)

c) df.clean_null(value)  # Not a standard method

d) df.set_null(value)

8. Which function is used to import the pandas library?

a) import numpy as pd

b) import pandas as pd 

c) from pandas import df

d) include pandas

9. How can you create a DataFrame from a CSV file?

a) df = pd.read_csv('data.csv')

b) df = pd.DataFrame.from_csv('data.csv')

c) df = open('data.csv')  # Incorrect approach

d) df = pd.parse_csv('data.csv')

10. Which of the following function can be used to delete a DataFrame column with missing value?

a) df.dropna(axis='columns')

b) df.delete()

c) df.dromna(axis='rows')

d) df.dropna(axis='dataframe')




ANSWERS: Python Pandas DataFrame MCQs:

1. b) A multi-dimensional labeled data structure with potentially different data types in each column

2. b) df = pd.DataFrame(dict, index=index_list)

3. b) df['column_name']

4. c) df[condition]

5. c) df.describe()

6. c) df.dropna()

7. a) df.fillna(value)

8. b) import pandas as pd 

9. a) df = pd.read_csv('data.csv')

10. a) df.dropna(axis='columns')

 

Back to Python MCQs >

Sunday 17 March 2024

Common Pandas Operations with Examples


Popular Pandas Operations with Examples

If you want to use Pandas in your data science project, then learn followings common pandas operations:

  1. Creating Data
  2. Accessing Data
  3. Data Selection
  4. Data Manipulation
  5. Data Aggregation and Summarization
Let's explore these operations with examples:

1. Creating Data

Let's create Series and DataFrame type of variables to apply various Pandas Operations on it.  

Creating a Series

import pandas as pd

# Prepare data variable
data = [1, 2, 3, 4, 5]

# Creating a Series
my_series = pd.Series(data)

# Print a Series
print(my_series)

Output:

0 1
1 2
2 3
3 4
4 5
dtype: int64
  
Creating a DataFrame

# Create dictionaries for each column
data = {
    'Name': ['Kuntal', 'Sunit', 'Tapan', 'Amit'],
    'Age': [40, 38, 42, 44],
    'Bank': ['SBI', 'HDFC', 'ICICI', 'KOTAK']
}

# Create a DataFrame
df = pd.DataFrame(data)

# Print the DataFrame
print(df)

Output:

Name Age Bank 0 Kuntal 40 SBI 1 Sunit 38 HDFC 2 Tapan 42 ICICI 3 Amit 44 KOTAK


2. Accessing Data

Popular way to access data are:
  • .head() : To view the first few rows.
  • .tail() : To view the last few rows.
  • [ ] : Access columns or rows by labels or positions (similar to list indexing).
Examples:

# To view first 2 rows from DataFrame "df"

df.head(2)

Output:

Name Age Bank 0 Kuntal 40 SBI 1 Sunit 38 HDFC

# To print last 2 rows from DataFrame "df"

df.tail(2)

Output:

    Name    Age Bank
2   Tapan   42  ICICI
3   Amit    44  KOTAK


3. Data Selection

Use following functions for data selection.

  • .loc[ ]: Select rows/columns based on labels.
  • .iloc[ ]: Select rows/columns based on positions (zero-based indexing).
  • Boolean filtering for conditional selection.
Examples:

print(df.loc[df['Age'] > 40])  # Select rows where 'Age' > 40

Output:

Name Age Bank 2 Tapan 42 ICICI 3 Amit 44 KOTAK

print(df.iloc[1:3])  # Select rows from index 1 (inclusive) to 3 (exclusive)

Output:

Name Age Bank 1 Sunit 38 HDFC 2 Tapan 42 ICICI


4. Data Manipulation


Following data manipulation operations can be done on data:
  • Add, remove, or rename columns.
  • Create new columns based on calculations.
  • Handle missing values.
Example:

To rename a given column, write following code:

# To rename "Age" column with "Years Old" in "df" variable
df.rename(columns={'Age': 'Years Old'}, inplace=True)
print(df)

Output:

Name Years Old Bank 0 Kuntal 40 SBI 1 Sunit 38 HDFC 2 Tapan 42 ICICI 3 Amit 44 KOTAK


5. Data Aggregation and Summarization

  • Calculate descriptive statistics (mean, median, standard deviation, etc.).
  • Group data and perform aggregate operations (sum, count, average).
Example:

print(df.describe())  

Output:

Years Old count 4.000000 mean 41.000000 std 2.581989 min 38.000000 25% 39.500000 50% 41.000000 75% 42.500000 max 44.000000

where, 25%, 50% and 75% are Percentile,  it means how many of the values are less than the given percentile.


Introduction to Python Pandas


Introduction to Python Pandas


Pandas is a fundamental library for data manipulation in Python! Here's a quick introduction with some examples to get you started:

What is pandas?


Pandas is a powerful open-source library for data analysis in Python. It provides high-performance, easy-to-use data structures and data manipulation tools for working with tabular data like spreadsheets and databases.

It offers a wide range of features to:
  • Import data from various sources like CSV files, Excel spreadsheets, databases, and more.
  • Clean and prepare data by handling missing values, duplicates, and other inconsistencies.
  • Explore and analyze data using descriptive statistics, grouping, filtering, and sorting operations.
  • Perform data transformations by creating new columns, modifying existing data, and reshaping the data structure.
  • Visualize data using integration with libraries like Matplotlib and Seaborn for creating informative charts and graphs.

Why use pandas?


Some of the key programming activity that you can perform using pandas are listed below:
  • Data Cleaning and Manipulation: Pandas offers functions to clean messy data, handle missing values, and reshape your data for analysis.
  • Data Analysis: You can perform various data analysis tasks like calculating statistics, grouping data, and aggregating results.
  • Data Visualization: Pandas integrates well with plotting libraries like Matplotlib and Seaborn for data visualization.

Core Data Structures:


  • Series: A one-dimensional array labeled with data (similar to a spreadsheet column).
  • DataFrame: A two-dimensional labeled data structure with rows and columns (like a spreadsheet).

Getting started with pandas:

  • Installation: If you don't have pandas installed, use pip:
pip install pandas  
  • Import pandas:
import pandas as pd

Example 1: Creating a Pandas Series

# Prepare data variable
data = [1, 2, 3, 4, 5]

# Creating a Series
my_series = pd.Series(data)
print(my_series)

Output:
0 1
1 2
2 3
3 4
4 5
dtype: int64

Example 2: Creating a Pandas DataFrame

# Prepare data in dictionaries format
data = {
    'Name': ['Kuntal', 'Sunit', 'Tapan', 'Amit'],
    'Age': [40, 38, 42, 44],
    'Bank': ['SBI', 'HDFC', 'ICICI', 'KOTAK']
}

# Create a DataFrame
df = pd.DataFrame(data)

# Print the DataFrame
print(df)

Output:
Name Age Bank 0 Kuntal 40 SBI 1 Sunit 38 HDFC 2 Tapan 42 ICICI 3 Amit 44 KOTAK

Recommended Article for you:

    Friday 8 March 2024

    Python program to replace given words from a text file.

    Python - Replace Word Program

    Following Python program will replace a given word from a text file.

    Example: Assume that we have a students.txt file with a following content in it:

    This is a java programming class.
    We will develop an Insurance Calculator using java.
    Let's explore java syntax during today's lab.

    Following program will replace java with python in the "students.txt" file.

    # Python program to replace given words from a text file.

    old_word = 'python'
    new_word = 'java'

    with open("students.txt", 'r') as file:
        contents = file.read()
        contents = contents.replace(old_word, new_word)

    with open("students.txt", 'w') as file:
        file.write(contents)

    print(f'Replaced "{old_word}" with "{new_word}" in students.txt.

    Sample output:

    Replaced "java" with "python" in students.txt.


    Content of a students.txt file after execution of a program:

    This is a python programming class.
    We will develop an Insurance Calculator using python.
    Let's explore python syntax during today's lab.