Untitled

 avatar
unknown
plain_text
5 months ago
2.8 kB
12
Indexable
Q1


import pandas as pd

# Load the dataset (if not already loaded)
df = pd.read_csv('/path/to/your/file.csv')

# Convert 'Order Date' to datetime format
df['Order Date'] = pd.to_datetime(df['Order Date'], format='%d/%m/%Y')

# Filter sales data for the year 2018
sales_2018 = df[df['Order Date'].dt.year == 2018]

# Display all sales data for 2018
sales_2018_data = sales_2018[['Order Date', 'Sales']]
print(sales_2018_data)



Q2


import pandas as pd

# Load the dataset (if not already loaded)
df = pd.read_csv('/path/to/your/file.csv')

# Find the product with the highest sales
top_product = df.loc[df['Sales'].idxmax()]

# Display the product name and sales
print("Product with highest sales:")
print("Product Name:", top_product['Product Name'])
print("Sales:", top_product['Sales'])

Q3


import pandas as pd

# Load the dataset (if not already loaded)
df = pd.read_csv('/path/to/your/file.csv')

# Count the frequency of each country
country_counts = df['Country'].value_counts()

# Display the frequency of countries
print(country_counts)


Q4


import pandas as pd
import matplotlib.pyplot as plt

# Load the dataset (if not already loaded)
df = pd.read_csv('/path/to/your/file.csv')

# Convert 'Order Date' to datetime format
df['Order Date'] = pd.to_datetime(df['Order Date'], format='%d/%m/%Y')

# Extract year and month from 'Order Date'
df['Year'] = df['Order Date'].dt.year
df['Month'] = df['Order Date'].dt.month

# Group by year and month, then sum the sales
monthly_sales = df.groupby(['Year', 'Month'])['Sales'].sum().reset_index()

# Pivot the data to make months as columns
monthly_sales_pivot = monthly_sales.pivot(index='Year', columns='Month', values='Sales')

# Plotting the data
plt.figure(figsize=(10, 6))
monthly_sales_pivot.plot(kind='bar', stacked=True, figsize=(12, 6))
plt.title('Total Sales by Month for Each Year')
plt.xlabel('Year')
plt.ylabel('Total Sales')
plt.xticks(rotation=45)
plt.legend(title='Month', labels=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
plt.tight_layout()
plt.show()

Q5

import pandas as pd
import matplotlib.pyplot as plt

# Load the dataset (if not already loaded)
df = pd.read_csv('/path/to/your/file.csv')

# Convert 'Order Date' to datetime format
df['Order Date'] = pd.to_datetime(df['Order Date'], format='%d/%m/%Y')

# Extract year from 'Order Date'
df['Year'] = df['Order Date'].dt.year

# Find the highest sales for each year
highest_sales_per_year = df.groupby('Year').apply(lambda x: x.loc[x['Sales'].idxmax()])

# Plotting the scatter plot
plt.figure(figsize=(10, 6))
plt.scatter(highest_sales_per_year['Year'], highest_sales_per_year['Sales'], color='red')

# Add labels and title
plt.title('Highest Sales of Each Year')
plt.xlabel('Year')
plt.ylabel('Highest Sales')

# Show the plot
plt.tight_layout()
plt.show()
Editor is loading...
Leave a Comment