Page
Import Library
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltডেটা লোড করি
df =pd.read_csv(r'C:\Users\oleetech\Downloads\purchase.csv',encoding='unicode_escape')ডেটা Convert করি
# Convert the 'Total Amount' column to numeric
df['Total Amount'] = df['Total Amount'].str.replace(',', '').astype(float)
# Convert the 'Posting Date' column to a datetime format
df['Posting Date']= pd.to_datetime(df['Posting Date'])Define Date
# Define the date range
start_date="2021-04-10"
end_date="2022-12-31"Filter Dataframe
# filter dataframe between date
filtered_df= df[(df['Posting Date'] >= start_date ) & (df['Posting Date'] <= end_date )]আইটেম কে purchase এমাউন্ট এর ভিত্তিতে group করি
group_data = filtered_df.groupby('Item Description')['Total Amount'].sum().reset_index()# Sort by 'Total Amount' to find the top 1 item
top_item = group_data.sort_values(by='Total Amount', ascending=False).iloc[1]['Item Description']
# Filter the DataFrame to select only the rows corresponding to the top item
top_item_history = filtered_df[filtered_df['Item Description'] == top_item]# Sort by 'Posting Date' to create a chronological history of prices
top_item_history = top_item_history.sort_values(by='Posting Date')# Extract year and month from 'Posting Date' and create a new column
top_item_history['YearMonth'] = top_item_history['Posting Date'].dt.to_period('M')# Group by 'YearMonth' and calculate unique Price USD values for each month
monthly_unique_prices = top_item_history.groupby('YearMonth')['Price USD'].unique()# Convert the Series to a DataFrame
monthly_unique_prices_df = monthly_unique_prices.reset_index()# Convert 'YearMonth' column to strings for plotting
monthly_unique_prices_df['YearMonth'] = monthly_unique_prices_df['YearMonth'].astype(str)
# Sort the DataFrame by 'YearMonth'
monthly_unique_prices_df = monthly_unique_prices_df.sort_values(by='YearMonth')flattened_prices = []
for sublist in monthly_unique_prices_df['Price USD']:
flattened_prices.extend(sublist)
# Create a new DataFrame with flattened prices
df_flattened = pd.DataFrame({
'YearMonth': np.repeat(monthly_unique_prices_df['YearMonth'], monthly_unique_prices_df['Price USD'].apply(len)),
'Price USD': np.concatenate(monthly_unique_prices_df['Price USD'])
})# Create a bar chart
plt.figure(figsize=(10, 6))
plt.bar(df_flattened['YearMonth'], df_flattened['Price USD'])
plt.xlabel('Year-Month')
plt.ylabel('Price USD')
plt.title('Price increasing History ')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Last updated