Case Base Practice Question
Case Base Practice Question
import pandas as pd
data = {
df = pd.DataFrame(data)
How do you show the 'Price' and 'Stock' columns from the DataFrame df?
a) df[['Price', 'Stock']]
b) df['Price']
c) df['Stock']
d) df.loc['Price', 'Stock']
How do you find the details of a product named 'Smartphone' in the DataFrame df?
a) df[df.Product == 'Smartphone']
b) df['Product']
c) df.Product['Smartphone']
d) df.loc[1]
a) df.tail(1)
b) df.head(1)
c) df.head()
d) df.iloc[0]
Answer: b) df.head(1)
Add a new column named 'Category' with the value 'Electronics' for all products.
a) df['Category'] = 'Electronics'
b) df('Category') = 'Electronics'
c) df.loc['Category'] = 'Electronics'
d) df['Category'] = 'Electronics'
How can you add a column named 'Discount' with different values for each product in the
DataFrame?
b) df['Discount'] = 'Discount'
Change the index of the DataFrame and set the new index names:
c) df[df['Stock'] == 20]
a) df['Price'].max()
b) df['Price'].min()
c) df.max()['Price']
d) df['Price'].mean()
Answer: a) df['Price'].max()
a) df['Stock'].mean()
b) df['Stock'].sum()
c) df['Stock'].median()
d) df['Stock'].std()
Answer: a) df['Stock'].mean()
b) df.pop('Discount')
c) df.remove('Discount')
d) df['Discount'] = None
Answer: a) df.drop('Discount', axis=1, inplace=True)
import pandas as pd
data = {
df = pd.DataFrame(data)
Questions
11. How do you show the 'Math' and 'Science' columns from the DataFrame df?
a) df[['Math', 'Science']]
b) df['Math']
c) df['Science']
d) df.loc['Math', 'Science']
12. How do you find the details of a student named 'Anna' in the DataFrame df?
a) df[df.Student == 'Anna']
b) df['Student']
c) df.Student['Anna']
d) df.loc[1]
a) df.tail(1)
b) df.head(1)
c) df.iloc[-1]
d) df.last()
Answer: a) df.tail(1)
14. Add a new column named 'Grade' with the value 'A' for all students.
a) df['Grade'] = 'A'
b) df('Grade') = 'A'
c) df.loc['Grade'] = 'A'
d) df['Grade'] = 'A'
15. How can you add a column named 'Total' that sums up the scores in 'Math',
'English', and 'Science'?
a) df.set_index('Student', inplace=True)
b) df.index = df['Student']
c) df.set_index(['Student'])
d) df.index = ['John', 'Anna', 'Mike']
17. Select rows where the 'Math' score is greater than 85.
a) df['English'].mean()
b) df['English'].sum()
c) df['English'].median()
d) df['English'].std()
Answer: a) df['English'].mean()
a) df['Science'].max()
b) df['Science'].min()
c) df.max()['Science']
d) df['Science'].mean()
Answer: a) df['Science'].max()
DataFrame Setup
python
import pandas as pd
Questions
python
print(df1)
print(df2)
python
python
4. Write the command to rename column 'A' to 'Alpha' in both DataFrames df1
and df2.
python
df1.rename(columns={'A': 'Alpha'}, inplace=True)
df2.rename(columns={'A': 'Alpha'}, inplace=True)
5. Write the command to change index labels of df1 from 0 to 'first', 1 to 'second',
and 2 to 'third'.
python
python
print(df2.columns)
7. Write the command to get the maximum value from column 'B' in DataFrame
df1.
python
max_B_df1 = df1['B'].max()
8. Write the command to find the mean of column 'Alpha' in DataFrame df2 after
renaming.
python
mean_Alpha_df2 = df2['Alpha'].mean()
9. Write the command to get the descriptive statistics (like mean, standard deviation,
etc.) of DataFrame df1.
df1_stats = df1.describe()
10. Write the command to find the sum of each column in DataFrame df2.
sum_df2 = df2.sum()