Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a bar plot of the 'sales' column.
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]} df = pd.DataFrame(data) df['sales'].[1]() plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using plot.line instead of plot.bar
Forgetting to call the plot method
Using plot.hist which creates a histogram
✗ Incorrect
The 'plot.bar' method creates a bar plot of the selected column.
2fill in blank
mediumComplete the code to set the title of the bar plot to 'Sales Data'.
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]} df = pd.DataFrame(data) ax = df['sales'].plot.bar() ax.[1]('Sales Data') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using xlabel or ylabel instead of title
Trying to set title on plt instead of ax
Forgetting to call the method
✗ Incorrect
The 'title' method sets the title of the plot.
3fill in blank
hardFix the error in the code to correctly plot a bar chart with product names as x-axis labels.
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]} df = pd.DataFrame(data) df.plot.bar(x=[1], y='sales') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sales' as x-axis labels
Using 'index' which is default row numbers
Using a non-existent column name
✗ Incorrect
The 'x' parameter should be the column with labels, which is 'product'.
4fill in blank
hardFill both blanks to create a horizontal bar plot with 'product' on y-axis and 'sales' on x-axis.
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]} df = pd.DataFrame(data) df.plot.barh(x=[1], y=[2]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping x and y parameters
Using 'index' instead of 'product'
Using non-existent columns
✗ Incorrect
For horizontal bar plots, x is the label column 'product' on y-axis, y is 'sales' on x-axis.
5fill in blank
hardFill all three blanks to create a bar plot with 'product' as x-axis, 'sales' as y-axis, and set the color to 'green'.
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]} df = pd.DataFrame(data) ax = df.plot.bar(x=[1], y=[2], color=[3]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column names for x or y
Using color names not supported
Forgetting to put quotes around strings
✗ Incorrect
Set x='product', y='sales', and color='green' to customize the bar plot.