Appearance
question:Generates a bar chart representing the distribution of specified items across given locations. The function takes a DataFrame with 'Item' and 'Location' columns and plots the count of each item per location. If lists of items and locations are provided, the chart will only include those specified, otherwise it defaults to a predefined list. The function should raise the exception for: ValueError: If 'df' is not a DataFrame, or if 'Item' or 'Location' columns are missing. The function should output with: matplotlib.axes.Axes: Axes object with the plotted bar chart. You should write selfcontained code starting with: ``` import pandas as pd import matplotlib.pyplot as plt def task_func(df, items=None, locations=None): ```
answer:if not isinstance(df, pd.DataFrame) or not all(col in df.columns for col in ['Item', 'Location']): raise ValueError("Invalid 'df': must be a DataFrame with 'Item' and 'Location' columns.") items = items or ['apple', 'banana', 'grape', 'orange', 'pineapple'] locations = locations or ['store1', 'store2', 'store3', 'store4', 'store5'] item_count_df = df.groupby(['Location', 'Item']).size().unstack().fillna(0) ax = item_count_df.plot(kind='bar', stacked=True) ax.set_title('Item Distribution by Location') ax.set_ylabel('Count') plt.show() return ax
question:Draw and return the daily turnover line chart from a pandas DataFrame. The function should raise the exception for: ValueError: If 'df' is not a DataFrame or lacks 'Date' or 'Sales' columns, or has no data to plot. The function should output with: Axes: Matplotlib Axes object with the line chart. You should write selfcontained code starting with: ``` import pandas as pd import matplotlib.pyplot as plt def task_func(df): ```
answer:if not isinstance(df, pd.DataFrame) or not all(col in df.columns for col in ['Date', 'Sales']): raise ValueError("Invalid 'df': must be a DataFrame with 'Date' and 'Sales' columns.") df['Date'] = pd.to_datetime(df['Date']) df = df.set_index('Date') resampled_df = df.resample('D').sum() if resampled_df.empty or resampled_df['Sales'].sum() == 0: raise ValueError("No data available to plot after resampling.") ax = resampled_df.plot(y='Sales') ax.set_title('Daily Turnover') ax.set_ylabel('Sales') plt.show() return ax
question:Draw and return a heat map with temperature data from a pandas DataFrame. The function should raise the exception for: ValueError: If 'df' is not a DataFrame or lacks 'Date', 'Time', or 'Temperature' columns. The function should output with: Axes: Seaborn heatmap object. You should write selfcontained code starting with: ``` import pandas as pd import seaborn as sns def task_func(df): ```
answer:if not isinstance(df, pd.DataFrame) or not all(col in df.columns for col in ['Date', 'Time', 'Temperature']): raise ValueError("Invalid 'df': must be a DataFrame with 'Date', 'Time', and 'Temperature' columns.") df['Date'] = pd.to_datetime(df['Date']) df['Month'] = df['Date'].dt.month df['Day'] = df['Date'].dt.day df_pivot = df.pivot(index="Month", columns="Day", values="Temperature") ax = sns.heatmap(df_pivot) ax.set_title('Temperature Heatmap') return ax
question:Draws a pie chart of the status distribution from a pandas DataFrame with a 'Status' column and returns the plot object. The 'Status' column in the DataFrame is expected to contain categorical data with possible values like 'Pending', 'In Progress', 'Completed', 'Cancelled'. The function should raise the exception for: ValueError: If 'df' is not a pandas DataFrame or does not contain the 'Status' column. The function should output with: matplotlib.axes.Axes: The Axes object with the pie chart. You should write selfcontained code starting with: ``` import pandas as pd import matplotlib.pyplot as plt def task_func(df): ```
answer:if not isinstance(df, pd.DataFrame) or 'Status' not in df.columns: raise ValueError("Input must be a pandas DataFrame with a 'Status' column.") status_counts = df['Status'].value_counts() fig, ax = plt.subplots() ax.pie(status_counts, labels=status_counts.index, autopct='%1.1f%%') ax.set_title('Status Distribution') return ax