This assignment contains various questions focusing on how to create a DataFrame with different options. It is an important topic for Class XII CBSE Boards Students and through this assignment they will learn about the various concepts associated with the Creation of DataFrame easily.
Question : What is Dictionary in Python?
Answer : In Python, a dictionary is a built-in data structure that stores a collection of key-value pairs. Each key is unique and maps to a specific value, and you can use the keys to look up the corresponding values.
A dictionary is similar to a map in other programming languages, and it is often used to store and access data in an efficient and convenient way.
Example :
In the above example, “phone_diary” is a dictionary, where it is a composition of Keys (‘Mahesh’, ‘Raj’, ‘Rahul’, ‘Raju’) with their values (956085, 981135, 976585, 999992).
Question : What is DataFrame in Python?
Answer : A DataFrame is a two-dimensional data structure in Python, designed to handle tabular data with labeled axes (rows and columns). It is a core component of the Pandas library and is one of the most widely used data structures for data analysis and manipulation.
A DataFrame is similar to a spreadsheet or a SQL table, and it allows you to perform a wide range of operations on your data, such as:
- Selecting, filtering, and aggregating data
- Sorting and rearranging data
- Joining and merging data from multiple sources
- Grouping and summarizing data
- Cleaning and transforming data
A DataFrame is made up of rows and columns, where each column is a series of data of the same data type. The rows and columns are labeled, and you can access and manipulate data based on these labels.
Question : How many ways you can create a DataFrame?
Answer : There are several ways to create a DataFrame in Python using the Pandas library:
1. By Using Dictionary :
a) Dictionary having values as a list :
As per the name suggests, in this type of dictionary, we take a list as a value for the key, instead of taking a single value for the key, then we create the DataFrame, for example :
number_of_students={‘section’:[‘A’,’B’,’C’,’D’],’students’:[50,45,38,54]}
df=pandas.DataFrame(number_of_students)
b) Dictionary having values as a dictionary :
In this type of dictionary, in place of taking a single value we take the dictionary as a value of a key and then we create the DataFrame. For example
people={‘Sales’:{‘name’:’Rohit’,’age’:24,’sex’:’Male’},’marketing’:{‘name’:’Raj’,’age’:28,’sex’:’Male’}}
df=pandas.DataFrame(people)
c) Dictionary having values as Series object
In this type of dictionary, firstly we need to create a Series object and then use these series objects as the value for the dictionary and then we create the DataFrame. For example :
ser1=pd.Series([‘Aman’,’Suman’,’Rahul’,’Mohit’])
ser2=pd.Series([78,98,86,92])
dic={‘Names’: ser1, ‘Marks’: ser2}
df=pandas.DataFrame(dic)
2. List of Dictionaries :
In this, first we create a list of dictionary, then create dataframe.
student1={‘Name’:’Yash’,’Percentage’:78}
student2={‘Name’:’Rahul’,’Percentage’:98}
student_list=[student1, student2]
df=pandas.DataFrame(student_list)
3. List of List:
This is a type of list containing another lists. You can create dataframe from this list. For example :
num_list=[[1,2,3],[4,5,6],[7,8,9]]
df=pandas.DataFrame(num_list)
4. 2-D ndArray:
You can also create a dataframe object by passing a two-dimensional NumPy array into it. For example:
arr1=numpy.array([1,2,3,4,5])
df=pandas.DataFrame(arr1)
5. Creating DataFrame from another DataFrame:
It is also possible to create one dataframe from the other one. For example :
people={‘Sales’:{‘name’:’Rohit’,’age’:24,’sex’:’Male’},’marketing’:{‘name’:’Raj’,’age’:28,’sex’:’Male’}}
df=pandas.DataFrame(people)
newDF=pandas.DataFrame(df)
Question : Create a dictionary that stores the mobile names list as value for ‘Mobiles’ key and their price list as value for ‘Price’ key. Create DataFrame from this dictionary.
Answer : In this question, first we need to create 2 lists and then use these 2 lists as a value of dictionary (refer line no 6 in below code).Question : Create and display a DataFrame from a 2D dictionary,’ Medals’, which stores the winner details as inner dictionary for “Basketball, Volleyball and Football”.
Answer : In this first we need to create Nested dictionary i.e. dictionary inside the dictionary, where inner dictionary contains the winner details and outer dictionary contains games names as key and winner’s detail as value.
Question : Write a program to create a DataFrame from a list containing dictionaries which contains “Salaries” and “Incentives” for 4 employees. Employee names should be the row labels.
Answer :
Question : Write code to create DataFrame from a list containing 4 dictionaries, where each dictionary containing Target and Actual Sales figures of four zonal offices. Give appropriate row labels
Answer :
Question : Write a program to create a DataFrame from a 2D array as shown below:
11 22 33
44 55 66
77 88 99
Answer :
Question : Write a code to create DataFrame object using an ndarray that has 5 elements.
Answer :
Question : Write a code to create DataFrame from 2D array (by ndarray) that has 9 elements.
Answer :
Question : Consider two series objects staff and salaries that store number of people in various office branches and salaries distributed in these branches, respectively. Write a program to create another Series object that stores average salary per branch and then create a DataFrame object from these Series objects.
Answer:
Question : Consider two series objects “total_stocks” and “available_stock” that store number of total number of stocks of different products and available stocks for the same items. Write a program to create another Series object that stores consumed stocks per products and then create a DataFrame object from these Series objects
Answer :
Question : Write a program to create a DataFrame to store weight, age and names of 3 people. Print the DataFrame.
Answer :
Question : Write a code to Create DataFrame from the below table.
Roll No | Name | Class | Percentage |
1 | Raj | XII | 89 |
2 | Amit | XII | 98 |
3 | Naman | XII | 78 |
4 | Yash | XII | 85 |
5 | Saiyam | XII | 80 |
Answer:
Question : How to read a csv file named “Student.csv” and create a DataFrame
Answer:
import pandas as pd
import csv
df=pd.read_csv(“Student.csv”)
Do you want to learn more and practice various types of questions?
Click on the below links:
- DataFrame Practice Questions with Solutions (Part-2) – for accessing DataFrame
- DataFrame Practice Questions with Solutions (Part-3) – for add/delete rows/columns into DataFrame.
- DataFrame Practice Questions with Solutions (Part-4) – for DataFrame attributes and functions.