0% found this document useful (0 votes)
10 views7 pages

Case Base Practice Question

hmmmm

Uploaded by

Ibrhm Farhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Case Base Practice Question

hmmmm

Uploaded by

Ibrhm Farhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Case base practice question

import pandas as pd

# Create a new DataFrame

data = {

'Product': ['Laptop', 'Smartphone', 'Tablet'],

'Price': [1200, 800, 400],

'Stock': [15, 30, 45]

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']

Answer: a) df[['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]

Answer: a) df[df.Product == 'Smartphone']

Display the details of the first product.

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'

Answer: a) df['Category'] = 'Electronics'

How can you add a column named 'Discount' with different values for each product in the
DataFrame?

a) df['Discount'] = [100, 50, 20]

b) df['Discount'] = 'Discount'

c) df['Discount'] = df['Price'] * 0.1

d) df['Discount'] = [10, 20]

Answer: a) df['Discount'] = [100, 50, 20]

Change the index of the DataFrame and set the new index names:

a) df.index = ["Product1", "Product2", "Product3"]

b) df.index(["Product1", "Product2", "Product3"])

c) df.index = ["Product1", "Product2"]

d) df.index = ["Product1", "Product2", "Product3", "Product4"]

Answer: a) df.index = ["Product1", "Product2", "Product3"]

Select rows where the 'Stock' is greater than 20.


a) df[df['Stock'] > 20]

b) df[df['Stock'] < 20]

c) df[df['Stock'] == 20]

d) df[df.Stock > 20]

Answer: a) df[df['Stock'] > 20]

Find the maximum price from the 'Price' column.

a) df['Price'].max()

b) df['Price'].min()

c) df.max()['Price']

d) df['Price'].mean()

Answer: a) df['Price'].max()

Get the average stock value from the 'Stock' column.

a) df['Stock'].mean()

b) df['Stock'].sum()

c) df['Stock'].median()

d) df['Stock'].std()

Answer: a) df['Stock'].mean()

Remove the 'Discount' column from the DataFrame.

a) df.drop('Discount', axis=1, inplace=True)

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

# Create a new DataFrame

data = {

'Student': ['John', 'Anna', 'Mike'],

'Math': [88, 92, 85],

'English': [90, 85, 88],

'Science': [95, 91, 89]

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']

Answer: a) df[['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]

Answer: a) df[df.Student == 'Anna']

13. Display the details of the last student.

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'

Answer: a) df['Grade'] = 'A'

15. How can you add a column named 'Total' that sums up the scores in 'Math',
'English', and 'Science'?

a) df['Total'] = df[['Math', 'English', 'Science']].sum(axis=1)


b) df['Total'] = df['Math'] + df['English'] + df['Science']
c) df['Total'] = df[['Math', 'English', 'Science']].mean(axis=1)
d) df['Total'] = df[['Math', 'English', 'Science']].sum(axis=0)

Answer: a) df['Total'] = df[['Math', 'English',


'Science']].sum(axis=1)

16. Change the index of the DataFrame to the 'Student' names.

a) df.set_index('Student', inplace=True)
b) df.index = df['Student']
c) df.set_index(['Student'])
d) df.index = ['John', 'Anna', 'Mike']

Answer: a) df.set_index('Student', inplace=True)

17. Select rows where the 'Math' score is greater than 85.

a) df[df['Math'] > 85]


b) df[df['Math'] < 85]
c) df[df['Math'] == 85]
d) df[df.Math > 85]

Answer: a) df[df['Math'] > 85]

18. Find the average score in 'English'.

a) df['English'].mean()
b) df['English'].sum()
c) df['English'].median()
d) df['English'].std()

Answer: a) df['English'].mean()

19. Get the highest score in 'Science'.

a) df['Science'].max()
b) df['Science'].min()
c) df.max()['Science']
d) df['Science'].mean()
Answer: a) df['Science'].max()

20. Remove the 'Grade' column from the DataFrame.

a) df.drop('Grade', axis=1, inplace=True)


b) df.pop('Grade')
c) df.remove('Grade')
d) df['Grade'] = None

Answer: a) df.drop('Grade', axis=1, inplace=True)

DataFrame Setup
python

import pandas as pd

# Create DataFrame df1


data1 = {
'A': [10, 20, 30],
'B': [40, 50, 60]
}
df1 = pd.DataFrame(data1)

# Create DataFrame df2


data2 = {
'A': [5, 15, 25],
'B': [35, 45, 55]
}
df2 = pd.DataFrame(data2)

Questions

1. Write the command to display both DataFrames df1 and df2.

python

print(df1)
print(df2)

2. Write the command to add DataFrames df1 and df2.

python

df_sum = df1 + df2

3. Write the command to subtract DataFrame df2 from df1.

python

df_diff = df1 - df2

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

df1.index = ['first', 'second', 'third']

6. Write the command to display the column names of DataFrame df2.

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

You might also like