Informatics Practices
Predict the output of following code (it uses below given dictionary my_di).
my_di = {"name" : ["Jiya", "Tim", "Rohan"],
"age" : np.array([10, 15, 20]),
"weight" : (75, 123, 239),
"height" : [4.5, 5, 6.1],
"siblings" : 1,
"gender" : "M"}
df = pd.DataFrame(my_di)
print(df)
Python Pandas
2 Likes
Answer
name age weight height siblings gender
0 Jiya 10 75 4.5 1 M
1 Tim 15 123 5.0 1 M
2 Rohan 20 239 6.1 1 M
Working
The given code creates a dictionary my_di. Then, a DataFrame df is created using the pd.DataFrame() constructor and passing the my_di dictionary. The print() function is used to display the DataFrame.
Answered By
3 Likes
Related Questions
From the DataFrames created in previous question, write code to add an empty column 'x' to all DataFrames.
What will be the output of the following program ?
import pandas as pd dic = {'Name' : ['Sapna', 'Anmol', 'Rishul', 'Sameep'], 'Agg' : [56, 67, 75, 76], 'Age' : [16, 18, 16, 19]} df = pd.DataFrame(dic, columns = ['Name', 'Age']) print(df)(a)
Name Agg Age 101 Sapna 56 16 102 Anmol 67 18 103 Rishul 75 16 104 Sameep 76 19(b)
Name Agg Age 0 Sapna 56 16 1 Anmol 67 18 2 Rishul 75 16 3 Sameep 76 19(c)
Name 0 Sapna 1 Anmol 2 Rishul 3 Sameep(d)
Name Age 0 Sapna 16 1 Anmol 18 2 Rishul 16 3 Sameep 19Consider the same dictionary my_di in the previous question (shown below), what will be the output produced by following code ?
my_di = {"name" : ["Jiya", "Tim", "Rohan"], "age" : np.array([10, 15, 20]), "weight" : (75, 123, 239), "height" : [4.5, 5, 6.1], "siblings" : 1, "gender" : "M"}df2 = pd.DataFrame(my_di, index = my_di["name"]) print(df2)Assume that required libraries (panda and numpy) are imported and dataframe df2 has been created as per questions 17 and 18 above. Predict the output of following code fragment :
print(df2["weight"]) print(df2.weight['Tim'])