text_html_output_1.png text_plain_output_1.png
import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import numpy as np
import pandas as pd
df = pd.read_csv('/kaggle/input/videogamesales/vgsales.csv')
df

most_common_pub = df.groupby('Publisher').count().sort_values(by='Rank', ascending=False).head(1).index[0]
most_common_pub

most_common_platform = df.groupby('Platform').count().sort_values(by='Rank', ascending=False).head(1).index[0]
most_common_platform

most_common_genre = df.groupby('Genre').count().sort_values(by='Rank', ascending=False).head(1).index[0]
most_common_genre

top_games = df.sort_values('Global_Sales', ascending=False)

# north_american_median = df['NA_Sales'].median()
# print(north_american_median)
# north_american_median_index = df.index[df.NA_Sales == north_american_median][0]
# print(df.iloc[north_american_median_index-5:north_american_median_index+5]['Name'])

median_value  = df["NA_Sales"].median()
df_sorted = df.iloc[(df['NA_Sales'] - median_value).abs().argsort()][::-1].reset_index(drop=True)

diff = (df['NA_Sales'] - median_value).abs()

df_sorted = df.assign(diff=diff).sort_values(['diff', 'NA_Sales'])

above_median_indices = df_sorted[df_sorted['NA_Sales'] > median_value].head(5).index
below_median_indices = df_sorted[df_sorted['NA_Sales'] < median_value].head(5).index

new_df = df.loc[above_median_indices.union(below_median_indices)].sort_values("NA_Sales", ascending=False)

print(median_value)
new_df

top_seller = df.head(1)
top_seller

platform_avgs = df.groupby(by='Platform')['Global_Sales'].mean().sort_values(ascending=False)
platform_avgs

df.tail(1)