Informatics Practices

Consider 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)

Python Pandas

4 Likes

Answer

       name  age  weight  height  siblings gender
Jiya    Jiya   10      75     4.5         1      M
Tim      Tim   15     123     5.0         1      M
Rohan  Rohan   20     239     6.1         1      M

Working

The given code creates a dictionary my_di. Then, a DataFrame df2 is created using the pd.DataFrame() constructor and passing the my_di dictionary and the my_di["name"] list as the index. The print() function is used to display the DataFrame.

Answered By

1 Like


Related Questions