CBSE Class 12 Informatics Practices Question 128 of 167

Python Pandas — I — Question 18

Back to all questions
18
Question

Question 15(a)

From the DataFrames created in previous question, write code to display only row 'a' from DataFrames df, df1, and df2.

Solution
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'])
print(df.loc['a',:])
print(df1.loc['a',:])
print(df2.loc['a',:])
Output
one    1.0
two    1.0
Name: a, dtype: float64
one    1.0
two    1.0
Name: a, dtype: float64
two      1.0
three    NaN
Name: a, dtype: object
Answer