-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
167 lines (129 loc) · 6.07 KB
/
Copy pathapp.py
File metadata and controls
167 lines (129 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv("movies.csv")
print(df.head())
print("Getting info of the dataset")
print(df.info())
print("Printing total null values of each column")
print(df.isna().sum())
# Dropping the rows with null values of score, votes, writer, country, runtime
df.dropna(subset=["score", "votes", "writer", "country", "runtime"], inplace=True)
print("Printing how many types of rating are there and how many of each of them is there")
print(df["rating"])
print(df["rating"].value_counts())
# First I thought that I will fill it with mode but then it might cause some problems as some movies might be restricted for some audience even if they are not incorrect then now I thought of dropping the rows
# Dropping the rows with null values of rating
df.dropna(subset=["rating"], inplace=True)
print("Checking how many null values are still there")
print(df.isna().sum())
# First checking whether there are any rows with budget or gross as zero
print("Checking whether budget has any zero value")
print(df[df["budget"] == 0])
print("Checking whether gross has any zero value")
print(df[df["gross"] == 0])
# Now filling the missing values in the dataset for budget
# First checking the distribution of the data
print("Printing the skewness of budget and then plotting budget")
print(df["budget"].skew())
sns.histplot(df["budget"].dropna(), kde=True)
plt.show()
# As the graph and the skew function suggest the data in the budget column so we will use median to fill it
df["budget"] = df["budget"].fillna(df["budget"].median())
print("Printing skewness of gross and plotting gross")
print(df["gross"].skew())
sns.histplot(df["gross"].dropna(), kde=True)
plt.show()
# Same case here that it is highly skewed data
# So now filling the gross with the median
df["gross"] = df["gross"].fillna(df["gross"].median())
# For the text data like company we cannot fill it with any data from our side as it is not a rule that which company might have produced this content
# So to handle these null values I will fill it with UNKNOWN
# I cannot leave these values as it is as if I do so they will not be used during plotting or groupby. I don't want to loose data so I will not delete them as well.
df["company"] = df["company"].fillna("UNKNOWN")
print("Again printing to see how many null values are still left")
print(df.isna().sum())
print("Column of stars")
print(df["star"])
print("\n")
print("Checking if there are any duplicated rows")
print(df.duplicated().sum())
# As only 1 row is missing so we can drop it
df = df.dropna(subset=["star"])
print("Finally printing that if all null values are fixed")
print(df.isna().sum())
# Adding new columns
df["profit"] = df["gross"] - df["budget"]
# As votes cannot be a float so I will convert them to integers to save memory
df["votes"] = df["votes"].astype("int")
df["runtime"] = df["runtime"].astype("int")
print("Printing dtypes of all columns")
print(df.dtypes)
decades = (df["year"] // 10) * 10
df.insert(loc=4, column="decade", value=decades)
print("Printing info of the dataframe")
print(df.info())
print("Printing decades column")
print(df["decade"])
# Now I am going to get the genre with highest IMDB average score
average_genre_score = df.groupby("genre")["score"].mean()
print("Getting the genre which has maximum IMDb score")
print(average_genre_score.idxmax())
sns.barplot(average_genre_score)
plt.xticks(rotation=45)
plt.show()
print(f"The category with highest average IMDb score is {average_genre_score.idxmax()}")
# Top 10 Directors by Average Score
# I am doing this because I don't want any director with only one movie which is super-hit to be classified as top director
movie_count = df["director"].value_counts()
qualified_directors = movie_count[movie_count > 3].index
average_director_score = df[df["director"].isin(qualified_directors)].groupby("director")["score"].mean()
print("Printing top director")
print(average_director_score.idxmax())
plt.figure(figsize=(10, 8))
sns.barplot(average_director_score.sort_values(ascending=False).head(20))
plt.xticks(rotation=45)
plt.show()
print(f"The top director with highest average IMDb score is {average_director_score.idxmax()}")
# Does runtime affect scores?
correlation = df["runtime"].corr(df["score"])
print("Correlation between runtime and score")
print(correlation)
# As we got 0.4 corr that means correlation is not very strong but still runtime and score are positively correlated
sns.regplot(x=df["runtime"], y=df["score"], line_kws={"color": "red", "lw": 4})
plt.show()
# Do bigger budgets earn more gross?
print("Correlation between budget and gross")
print(df["budget"].corr(df["gross"]))
# This value of corr suggests that they are highly and positively co-related
sns.regplot(x=df["budget"], y=df["gross"], color="green", line_kws={"color": "red", "lw": 4})
plt.show()
# Which genre makes the most money on average?
average_genre_gross = df.groupby("genre")["gross"].mean() / 1e6
print("Data is in millions")
print(average_genre_gross)
plt.figure(figsize=(10, 8))
sns.barplot(x=average_genre_gross.head(30).values, y=average_genre_gross.head(30).index)
plt.xlabel("Millions")
plt.ylabel("genre")
plt.show()
# Which country produces most movies?
movie_count_per_country = df["country"].value_counts().head(15) # I am using head although all countries can be displayed but on scale count will not be visible as the count of movies is very less
plt.figure(figsize=(10, 10))
plt.title("Top 15 countries by movie count from the dataset")
sns.barplot(y=movie_count_per_country.index, x=movie_count_per_country.values)
plt.xlabel("movie count")
plt.ylabel("country")
plt.show()
# Biggest flop
flop_movie = df.loc[df["profit"].idxmin()]
print(f"The biggest flop movie is {flop_movie['name']}")
# Most successful movie
success_movie = df.loc[df["profit"].idxmax()]
print(f"The most successful movie is {success_movie['name']}")
# Best decade quality by highest average score
score_mean_per_decade = df.groupby("decade")["score"].mean()
print(f"The best decade quality by highest average score is {score_mean_per_decade.idxmax()}")
sns.barplot(score_mean_per_decade)
plt.show()