text_html_output_1.png
import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

data = pd.read_csv('../input/onlineretail/OnlineRetail.csv', encoding='cp1252', header=0)

data = data.dropna()

a = data['Description'].str.split()
data['Type'] = a.apply(lambda x: ' '.join(x[-2:]))

Rev_Coustomer = data.groupby('CustomerID')['Revenue'].sum().sort_values(ascending=False).reset_index()[:15]

Rev_Country = data.groupby('Type')['Revenue'].sum().sort_values(ascending=False).reset_index()[:20]
plt.xticks(rotation=40, ha='right')

Rev_Country = data.groupby('InvoiceNo')['Revenue'].sum().sort_values(ascending=False).reset_index()[:20]
plt.xticks(rotation=40, ha='right')

grouped = data.groupby(['Type', 'CustomerID'])['Quantity'].sum().reset_index()
grouped = grouped.sort_values(by=['Type', 'Quantity'], ascending=[True, False])
grouped['Rank'] = grouped.groupby(['Type']).cumcount() + 1
Most_customer_Type = grouped[['Type', 'CustomerID', 'Quantity']][grouped['Rank'] == 1]
Most_customer_Type

a = data['Type'].unique()
df = pd.DataFrame(data=a.flatten()).reset_index()
df['Type'] = df[0]
df = df[['index', 'Type']]
df

data = data.merge(df, how='inner', on='Type')

data.rename(columns={'index': 'Type_ID'}, inplace=True)

Type_amount = data.groupby('Type_ID')['Revenue'].sum().reset_index()
Type_amount.rename(columns={'Revenue': 'Amount'}, inplace=True)
Type_amount

Type_Frequency = data.groupby('Type_ID')['Type'].count().reset_index()
Type_Frequency.columns = ['Type_ID', 'Frequency']
Type_Frequency

data