Important Questions For Python Panda 1 Class 12 Informatics Practices (IP)
Note:- Please Click on Question to get the Answer !!
Q1. What is Pandas library of Python? What is its significance ?
Q2. What do you understand by axes in a NumPy array ? Define axes for a 2d ndarray.
Q3. How is Series data structure different from a dataframe data structure ?
Q4. Given are two objects, a list object namely Ist1 and a Series object namely ser1, both are having similar values i.e., 2, 4, 6, 8.. Find out the output produced by following statements:
(i) print (1st1 * 2)
(ii) print (ser1 * 2)
Q5. Can you specify the reason behind the output produced by the code statements of question Given below?
Q6. Given a Series object s13 as shown below:
A 7600
B 5600
C 7000
D 7000
dtype: int64
Why is following code producing Error while working on Series object s13 ?
import pandas as pd
s13.index = range(0, 5)
print (s13)
Q7. Correct the error reported by the code of question Given below. Write corrected code for the same.
Q8. Consider the below given two code fragments. Will they produce the same output ? Why/why not?
(i)
fst = [9, 10, 11]
ob1 = pd. Series (data = fst * 2)
print (ob1)
(ii)
fst = pd. Series (data = [9, 10, 11])
ob2 = pd.Series (data = fst * 2)
print (ob2)
Q9. What will be the output of the following program?
import pandas as pd
fst = [9, 10, 11]
scd= pd.Series (fst)
ob1 = pd.Series (data = fst * 2)
ob2 = pd.Series (data = scd * 2)
print("ob1")
print (ob1)
print("ob2")
print (ob2)
Q10. What will be the output of the following code ?
import pandas as pd
import numpy as np
data = np.array(['al', 'b1', 'c1', 'd1', 'e1', 'f1'])
s = pd.Series (data))
print("I.")
print (s[:3])
print("II.")
print (s[-3:])
Q11. What will be the output of the following code ?
data = np.array(['a1', 'b1', 'c1', 'd1', 'e1'])
s = pd. Series (data, index = [1001,1002, 1003, 1004,1005])
# retrieve multiple elements with labels or index
print(s[[1002,1003,1004]])
Q12. Consider the following code to create two dataframes with similar values. What will be printed by the code given below? Justify your answer.
df1 = pd.DataFrame ([1, 2, 3])
df2 = pd.DataFrame([[1, 2, 3]])
print("df1")
print (df1)
print("df2")
print (df2)
Q13. How does dataframe object specify indexes to its data rows?
Q14. Given a data frame namely data as shown in adjacent table (fruit names are row labels). Write code statement to:
Color Count Price
Apple Red 3 120
Apple Green 9 110
Pear Red 25 125
Pear Green 26 150
Lime Green 99 70
(a) Find all rows with the label "Apple". Extract all columns.
(b) List fruits with count more than 25.
(c) List single True or False to signify if all prices are more than 100 or not.
(d) List 2nd, 3rd and 4th rows.
Q15. Consider the dataframe data given. Using the same dataframe data, answer the following:
Color Count Price
Apple Red 3 120
Apple Green 9 110
Pear Red 25 125
Pear Green 26 150
Lime Green 99 70
(a) List only the columns Color and Price using loc.
(b) List only columns 0 and 2 (column indexes) using iloc.
(c) List only rows with labels 'Apple' and 'Pear' using loc.
(d) List only rows 1, 3, 4 using iloc.
Q16. Consider the dataframe data given Below. Using the same dataframe data, answer the following:
Color Count Price
Apple Red 3 120
Apple Green 9 110
Pear Red 25 125
Pear Green 26 150
Lime Green 99 70
(a) Write statement to delete rows with labels 'Apple' and 'Lime".
(b) Write statement to delete columns with labels 'Color' and 'Count'.
Q17. Given a DataFrame mdf as shown below:
A B C
0 1 2 3
1 4 5 6
What will be the output produced by the following code ?
print("I :", mdf.iloc[0][0])
print("II :", mdf.loc[0]['C'])
print("III : ", mdf.at[1, 'A'])
print("IV : ", mdf.iat[1,2])
Q18. What is following statement doing?
df.drop(["Total", "Order"], axis = 1)
Q19. Given a DataFrame mdf as shown below:
A B C
0 1 2 3
1 4 5 6
Find out the errors in following statements.
(i) mdf.drop(["Total", "Order"], axis = 1)
(ii) mdf.drop (["A", "D"])
(iii) mdf.drop (["A", "D"], axis = 1)
Q20. Explain what the following statements are doing? df is the name of a DataFrame.
(i) df.iloc[:5,]
(ii) df.iloc [1:5,]
(iii) df.iloc [5,0]
(iv) df.iloc [1:5,0]
(v) df.iloc [1:5, :5]
(vi) df.iloc [2:7, 1:3]
Q21. Trying to extract the first five rows of DataFrame x, Nia has given code as:
x.loc[0:5]
Bur it is returning 6 rows. Why? Suggest the answer :-
Q22. What is following statement doing?
df.drop(df.columns [0], axis = 1)
Q23. What is the output of the following code?
import pandas as pd
import numpy as np
x = pd.DataFrame({"var1": np.arange (1,20,2)}, index = [9,8,7,6,10, 1, 2, 3, 4, 5])
x1 = x.iloc [4:7]
x2 = x.head (2)
x3= x.tail (4)
print("x1:")
x3= x.tail (4)
print("x1:")
print (x1)
print("x2:")
print (x2)
print("x3:")
print (x3)
Q24. What will the following code do ?
x = pd.DataFrame({"var1": np.arange (1, 20, 2)), index-[9, 8, 7, 6, 10, 1, 2, 3, 4, 5])
x1 = x.iloc [4:7]
x2 = x.head (2)
x3 = x. tail (4)
Q25. Why is the following code not renaming the index and columns even when code is executing without any error, considering that the sale Df dataframe is as shown on the right.
>>> saleDf
Target Sales
zoneA 56000 58000
zoneB 70000 68000
zoneC 75000 78000
zoneD 60000 61000
The code:
saleDf.rename(index = {'zoneC': 'Central', 'zoneD': 'Dakshin'},\
columns = {'Target': 'Targeted', 'Sales': 'Achieved'})
print( saleDf)
What output would be produced by the above code and what is the problem with the code?
Q26. What correction/modification would you suggest for the problem of the previous question which wanted to change the index and column name in the original data frame saleDf?
Q27. Consider the following code
Section = ['A', 'B', 'C']
Classes = [ 6, 4, 3]
dc = {'Section':Section, 'No. of Classes': Classes}
clasDf = pd.DataFrame (dc, index = ['True', 'False', 'True'])
print (clasdf.loc [True])
(i) Why is it giving KeyError if you run the given code?
(ii) Suggest the correction /solution for the above problem.
Q28. Given a Series that stores the area of some states in km². Write code to find out the biggest and smallest three areas from the given Series. Given series has been created like this:
Ser1 = pd.Series ( [34567, 890, 450, 67892, 34677, 78902, 256711, 678291, 637632, 25723, 2367, 11789, 345, 256517])
Q29. From the series Ser1 of areas (given earlier that stores areas of states in km2), find out the areas that are more than 50000 km².
Q30. Write a program to create a Series object with 6 random integers and having indexes as: ['p', 'q', 'r', 'n', 't', 'v']
Q31. Write a program to create data series and then change the indexes of the Series object in any random order.
Q32. Write a program to sort the values of a Series object s1 in ascending order of its values and store it into series object s2.
Q33. Write a program to sort the values of a Series object s1 in descending order of its indexes and store it into series object s3.
Q34. Given a Series object s4. Write a program to change the values at its 2nd row(index1) and 3rd row to 8000.
Q35. Given a Series object s5. Write a program to calculate the cubes of the Series values.
Q36. Given a Series object s5. Write a program to store the squares of the Series values in object s6. Display s6's values which are > 15.
Q37. Write a program to display number of rows and number of columns in DataFrame df.
Q38. Write a program to display number of rows and number of columns in DataFrame df without using shape attribute.
Q39. Given a DataFrame df:
Age Name Weight
0 15 Arnav 42
1 22 Charles 75
2 35 Guru 66
Write a program to display only the Weight of first and third rows.
Q40. What is a Pandas Series?
Q41. In Pandas, S is a series with the following result::
S = pd.Series ([5, 10, 15, 20, 25])
The series object is automatically indexed as 0, 1, 2, 3, 4. Write a statement to assign the series as a, b, c, d, e explicitly.
Q42. What is dataframe?
Q43. Dictionary S_marks contains the following data:
S_marks = {'name': ['Pashmi', 'Harsh', 'Ganesh', 'Priya', 'Vivek'], ‘Grade: [A1, A2, B1', 'A1', 'B2']}
Write a statement to create dataframe called df. Assume that Pandas has been imported as pd.
Q44. How can we check if a dataframe df has any missing values?
Q45. A dictionary Grade contains the following data:
Grade = {'Name': [ 'Rashmi', 'Harsh', 'Ganesh', 'Priya', 'Vivek, ‘Anita', 'Karthik'], ‘Grade': ['A1, A2, B1', 'A1', 'B2, A2', 'A1']]
Write statements for the following:
(a) Create a dataframe called Gr.
(b) Find the output of Gr.iloc[0:5) and Gr[0:5]
(c) Add a column called Percentage with the following data: [92, 89, None, 95, 68, None, 93].
(d) Rearrange the columns as Name, Percentage and Grade.
(e) Drop the column (i.e., Grade) by name.
(f) Delete the 3rd and 5th rows.
(g) What will the following codes do?
(i) Gr.drop(0, axis = 0)
(ii) Gr.drop(0, axis = "index")
(ii) Gr.drop([0, 1, 2, 3],axis = 0).
Q46. Write a Python code to create an empty dataframe.
Q47. How does a dataframe object specify indexes to its data rows?
Q48. Write a code statement to list the value of a cell in the 5th row and "item" column from a dataframe "sales".
Q49. Hitesh wants to display the last four rows of the dataframe df and has written the following code:
df.tail()
However, the last 5 rows are being displayed. Identify the error and rewrite the correct code so that only the last 4 rows get displayed.
Q50. Write the command using Insert() function to add a new column in the last place (3rd place) named "Salary" from the list Sal = [10000,15000,20000] in an existing dataframe named EMP already having 2 columns.
Q51. Write a Python code to create a dataframe with appropriate headings from the list given below:
['S101', 'Amy', 70], ['S102', 'Bandhi', 69], ['S104', 'Cathy', 75], ["S105', 'Gundaho', 82]
Q52. Write a small Python code to create a dataframe with headings (a and b) from the list given below:
[[1,2],[3,4],[5,6],[7,8]]
Q53. Who is the main author of Pandas?
Q54. What are the advantages of CSV file formats?
Q55. By default, read_csv() uses the values of the first row as column headers in dataframes. Which argument will you give to ensure that the top/first row's data is used as data and not as column headers?
Q56. Explain briefly the CSV format of storing files.
Q57. Write a program that reads from a CSV file where the separator character is '$'. Read only the first 5 rows in your dataframe. Give column headings as ItemName, Quantity, Price. Make sure to read the first row as data and not as column headers.
Q58. You want to read data from a CSV file in a dataframe but you want to provide your own column names to the dataframe. What additional argument would you specify in read_csv( )?
Q59. Which argument would you give to read_csv() if you only want to read the top 5 rows of data?
Q60. Write a command to store data of dataframe mdf into a CSV file Mydata.csv, with separate character as "@".
Q61. WAP to read details such as item, sales made in a dataframe and then store this data in a CSV file.
Q62. WAP to read data from a CSV file where separator character is "@". Make sure that the top row is used as data, not as column headers.
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )