Informatics Practices
From the DataFrames created in previous question, write code to display only row 'a' from DataFrames df, df1, and df2.
Answer
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
Related Questions
Carefully observe the following code :
import pandas as pd Year1 = {'Q1': 5000, 'Q2': 8000, 'Q3': 12000, 'Q4': 18000} Year2 = {'A': 13000, 'B': 14000, 'C': 12000} totSales = {1: Year1, 2: Year2} df = pd.DataFrame(totSales) print(df)Answer the following :
(i) List the index of the DataFrame df.
(ii) List the column names of DataFrame df.
Given :
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) print(df1) print(df2)What will Python show the result as if you execute above code ?
From the DataFrames created in previous question, write code to display only rows 0 and 1 from DataFrames df, df1, and df2.
From the DataFrames created in previous question, write code to display only rows 'a' and 'b' for columns 1 and 2 from DataFrames df, df1 and df2.