Worksheet Dataframe
Q1. What is a DataFrame?
Q2. Write code to create an empty DataFrame named ‘DF1’.
Q3. Which method is used to create a DataFrame in Python?
Q4. Write two differences between Series and DataFrame.
Q5. Create the following DataFrame using a list of dictionaries.
A | B | C |
---|---|---|
1 | 2 | 3 |
5 | 6 | 8 |
Q6. Which data types can be used to create a DataFrame?
Q7. Which library is imported to create DataFrame from NumPy arrays?
Q8. _______ method in Pandas is used to change/modify the index of rows and columns of a DataFrame.
Q9. Give an example to create a DataFrame from a single ndarray.
Q10. Give an example to create a DataFrame from two ndarrays.
Q11. Write the output of the following code:
pythonCopy codeimport numpy as np
import pandas as pd
A = np.array([35, 40, 71, 25])
B = np.array([27, 34, 56, 73])
C = [11, 22, 33, 44]
DF = pd.DataFrame([A, B, C])
print(DF)
Q12. Write the Python code to create a DataFrame from the given list:
pythonCopy codeL1 = ["Anil", "Ruby", "Raman", "Suman"]
L2 = [35, 56, 48, 85]
Q13. Fill in the blank to produce the following output:
pythonCopy codeimport pandas as pd
L1 = ["Anil", "Ruby", "Raman", "Suman"]
L2 = [35, 56, 48, 85]
DF = pd.DataFrame([L1, L2], ___________________________________ )
print(DF)
Output:
Ist | IInd | IIIrd | IVth |
---|---|---|---|
Anil | Ruby | Raman | Suman |
35 | 56 | 48 | 85 |
Q14. Aman stores data in the form of a nested list. He creates a DataFrame ‘DF’ from the list using the following code. How many columns are there in “DF”?
pythonCopy codeL1 = [["Aman", "Cricket", "7th"], ["Ankit", "Hockey", "11th"], ["Sunita", "Basketball", "9th"]]
import pandas as pd
DF = pd.DataFrame(L1)
print(DF)
Q15. Which attribute of DataFrame is used to set a user-defined index?
Q16. Which attribute of DataFrame is used to set a user-defined column name?
Q17. Complete the following code to get the output shown below:
pythonCopy codeimport pandas as ________________
L1 = [["Aman", 45], ["Ankit", 56], ["__________", 67]]
DF = pd.______________(L1, ______________=["Name", "Marks"], index=[__________])
print(DF)
Output:
Name | Marks |
---|---|
Aman | 45 |
Ankit | 56 |
Sunita | 67 |
Q18. Complete the following code to get the output shown below:
pythonCopy codeimport pandas as pd
L1 = {"Name" : ["Aman", "Ankit", "Sunita"], "Marks" : [45, 56, 67]}
DF = pd.DataFrame(L1, columns=[____________________], index=[1, 2, 3])
print(DF)
Output:
Marks | Name |
---|---|
45 | Aman |
56 | Ankit |
67 | Sunita |
Q19. Consider the code below and answer the questions:
pythonCopy codeLd = [{'a' : 10, 'b' : 20}, {'a' : 5, 'b' : 10, 'c' : 20}]
DF = pd.DataFrame(Ld)
print(DF)
a. How many rows are in the DataFrame “DF”?
b. How many columns are in the DataFrame “DF”?
c. How many NaN values are in the DataFrame “DF”?
d. Write the missing import statement in the code.
e. How many dictionaries are used in the code?
Q20. Give an example of creating a DataFrame from two Series.