# Top Vendor

**Import Library**&#x20;

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

**Load Data**

{% file src="<https://517141686-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPsBp4dN4Xe8LcTjaW2On%2Fuploads%2FJNEoFzZrdJBg0gjZ1RyY%2Fpurchase.csv?alt=media&token=728684d9-fb37-4ab0-a3fc-7d9f7d4e7457>" %}

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

<figure><img src="https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPsBp4dN4Xe8LcTjaW2On%2Fuploads%2FOknqO3oGBeC3JeuO8Wi2%2Fimage.png?alt=media&#x26;token=4c34fffc-7f55-4287-85cf-74bd464c80e5" alt=""><figcaption></figcaption></figure>

**Get Column info**

```python
print(df.columns)
```

```bash
Index(['Category', 'GRN no', 'Customer/Vendor Code', 'VendorName', 'Address',
       'LC No', 'Posting Date', 'Item No.', 'Item Description',
       'Inventory UoM', 'Quantity', 'Price', 'Price USD', 'Total Amount'],
      dtype='object')
```

**Convert Datatype**

```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 range**

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

**Filter Dataframe**

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

**Groupby and sort Dataframe**

```
result = filtered_df.groupby('VendorName')['Total Amount'].sum().reset_index()
top10vendors=result.sort_values(by='Total Amount',ascending=False).head(10)
```

**Create Chart**

```python
# Create a pie chart
plt.figure(figsize=(10,8))
plt.pie(top10vendors['Total Amount'],labels=top10vendors['VendorName'],autopct='%1.1f%%',startangle=140)
plt.axis('equal')
# plt.bar(top10vendors['VendorName'],top10vendors['Total Amount'])
# # Rotate the x-axis labels for better readability (optional)
# plt.xticks(rotation=90)
plt.title('Top 10 Vendor by Total Amount between date')
plt.savefig('top_vendors_chart.png', bbox_inches='tight')
plt.show()
```

<figure><img src="https://517141686-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPsBp4dN4Xe8LcTjaW2On%2Fuploads%2FmTQutQyOIVHWOuCaq34V%2Ftop_vendors_pie_chart.png?alt=media&#x26;token=b6cfaa30-1fb6-4fe3-8979-aa2ed1535f49" alt=""><figcaption></figcaption></figure>
