1.
What will be the output of the following code?
import pandas as pd
import numpy as np
arr1=np.array([[1,2,3],[4,5,6]])
df=pd.DataFrame(arr1,columns=['num1','num2','num3'])
print(df.iloc[0,2])
Explanation
The code imports the pandas and numpy libraries. It creates a NumPy array called arr1 with two rows and three columns. Then, it creates a DataFrame called df using the arr1 array and assigns column names 'num1', 'num2', and 'num3' to the DataFrame. Finally, it prints the value at the first row and third column of the DataFrame, which is 3.
2.
In Pandas, the method used to create a DataFrame from a dictionary is __________.
Explanation
In Pandas, pd.DataFrame is the method used to create a DataFrame from various data structures like dictionaries, lists, or arrays. A DataFrame is a two-dimensional, labeled data structure with columns that can hold different data types. It is one of the most commonly used data structures in Pandas, allowing for easy manipulation, analysis, and visualization of data.
3.
A dictionary grade contains the following
Grade={'Name':['Rashmi','Harsh','Ganesh','Priya','Vivek'],'Grade':['A1','A2','B1','A1','B2']}
Write the statement to add a column called 'Marks' with the following data.
[97,92,95,89,96]
use only single quotes in the answer as the software gives correct only for exact match
Explanation
The statement "Gr['Marks']=[97,92,95,89,96]" adds a column called 'Marks' to the dictionary grade and assigns the values [97,92,95,89,96] to it.
4.
Delete 3rd and 5th rows
Hint use drop method in a single statement
Explanation
The given code is using the "drop" method to delete the 3rd and 5th rows from the DataFrame "Gr". The drop method is being passed a list of indices [2,4] which corresponds to the 3rd and 5th rows. By calling the drop method on "Gr" with the specified indices, those rows are deleted from the DataFrame.
5.
Give the output
import pandas as pd
d={'one':pd.Series([1.,2.,3.],index=['a','b','c']), 'two':pd.Series([1.,2.,3.,4.],index=['a','b','c','d'])}
df=pd.DataFrame(d)
print(df)
Hint. only one space between columns and rows and start from the left and and leave one space from top margin
Explanation
The given code imports the pandas library and creates a dictionary 'd' with two keys 'one' and 'two'. Each key has a corresponding pandas Series as its value. The Series contain numerical values with specified indices. The code then creates a pandas DataFrame 'df' using the dictionary 'd'. Finally, it prints the DataFrame 'df'. The output shows the DataFrame with two columns 'one' and 'two', and the corresponding values from the Series. The indices are displayed on the left side.
6.
Give the output
import pandas as pd
d={'one':pd.Series([1.,2.,3.],index=['a','b','c']), 'two':pd.Series([1.,2.,3.,4.],index=['a','b','c','d'])}
df1=pd.DataFrame(d,index=['d','b','a'])
print(df1)
Hint. only one space between columns and rows and start from the left and top margin
Explanation
The code creates a DataFrame using the given dictionary 'd' and sets the index of the DataFrame to ['d','b','a']. The DataFrame has two columns, 'one' and 'two', which correspond to the values in the dictionary. The output shows the DataFrame with the specified index and the corresponding values from the dictionary.
7.
import pandas as pd
d={'one':pd.Series([1.,2.,3.],index=['a','b','c']), 'two':pd.Series([1.,2.,3.,4.],index=['a','b','c','d'])}
df2=pd.DataFrame(d,index=['d','a'],columns=['two','three'])
print(df2)
Explanation
The given code creates a DataFrame 'df2' using the dictionary 'd' with specified index and columns. The DataFrame has two columns 'two' and 'three'. However, since 'three' is not present in the dictionary 'd', the values for 'three' column are NaN (Not a Number). The values for 'two' column are 4.0 and 1.0 for index 'd' and 'a' respectively.
8.
Import pandas as pd
d={'one':pd.Series([1.,2.,3.],index=['a','b','c']),
'two':pd.Series([1.,2.,3.,4.],index=['a','b','c','d'])}
df=pd.DataFrame(d)
df1=pd.DataFrame(d,index=['d','b','a'])
df2=pd.DataFrame(d,index=['d','a'],columns=['two','three'])
Write code to
Display only column 'one' from dataframes df and df1, column 'two' from df2.
Hint: Write print statements one below the other
Explanation
The code is using print statements to display specific columns from the dataframes df, df1, and df2. It first prints the column 'one' from df, then the column 'one' from df1, and finally the column 'two' from df2.
9.
Import pandas as pd
d={'one':pd.Series([1.,2.,3.],index=['a','b','c']),
'two':pd.Series([1.,2.,3.,4.],index=['a','b','c','d'])}
df=pd.DataFrame(d)
df1=pd.DataFrame(d,index=['d','b','a'])
df2=pd.DataFrame(d,index=['d','a'],columns=['two','three'])
Write code to display only rows 0 and 1 from dataframes df, df1 and df2
Hint :use iloc
use print statements one below the other
Explanation
The code is displaying only rows 0 and 1 from dataframes df, df1, and df2 using the iloc function. The iloc function is used to select rows and columns by their integer position. In this case, [0:2,] is used to select rows 0 and 1. The print statements are used to display the selected rows for each dataframe.
10.
Import pandas as pd
d={'one':pd.Series([1.,2.,3.],index=['a','b','c']),
'two':pd.Series([1.,2.,3.,4.],index=['a','b','c','d'])}
df=pd.DataFrame(d)
df1=pd.DataFrame(d,index=['d','b','a'])
df2=pd.DataFrame(d,index=['d','a'],columns=['two','three'])
Write code to display only column one for rows b and c from df
Explanation
The code `print(df.loc['b':'c','one'])` is used to display only the values in column 'one' for the rows with index 'b' and 'c' from the DataFrame `df`. The `loc` function is used to access specific rows and columns based on their labels. In this case, the code selects rows 'b' to 'c' (inclusive) and column 'one' from the DataFrame `df` and prints the corresponding values.