# Page

##

{% file src="<https://517141686-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPsBp4dN4Xe8LcTjaW2On%2Fuploads%2FCCaVWXUxh47uHn2zHxZY%2FPrice%20Increasing.ipynb?alt=media&token=17baee61-e845-4a71-967f-7b6a7bca998d>" %}

## **Import Library**&#x20;

```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
```

## ডেটা লোড করি

```python
df =pd.read_csv(r'C:\Users\oleetech\Downloads\purchase.csv',encoding='unicode_escape')
```

## ডেটা Convert করি&#x20;

```python
# 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**

```python
# Define the date range
start_date="2021-04-10"
end_date="2022-12-31"
```

**Filter Dataframe**

```python
# filter dataframe between date
filtered_df= df[(df['Posting Date'] >= start_date ) & (df['Posting Date'] <= end_date )]
```

## আইটেম কে purchase এমাউন্ট এর ভিত্তিতে group করি

```python
group_data = filtered_df.groupby('Item Description')['Total Amount'].sum().reset_index()
```

```python
# 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]
```

```python
# Sort by 'Posting Date' to create a chronological history of prices
top_item_history = top_item_history.sort_values(by='Posting Date')
```

```python
# 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')
```

```python
# Group by 'YearMonth' and calculate unique Price USD values for each month
monthly_unique_prices = top_item_history.groupby('YearMonth')['Price USD'].unique()
```

```python
# Convert the Series to a DataFrame
monthly_unique_prices_df = monthly_unique_prices.reset_index()
```

```python
# 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')
```

```python
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'])
})
```

```python
# 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()
```

<figure><img src="https://517141686-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPsBp4dN4Xe8LcTjaW2On%2Fuploads%2FlnH3B74dRwejZb1BD18a%2Fimage.png?alt=media&#x26;token=9e209270-9c09-4872-aa6c-bc0f7a0cdc3e" alt=""><figcaption></figcaption></figure>
