import pandas as pd

youtube = pd.read_csv("/home/tkaratas/Python_Projects/py-Projects/Data Science with Python/DataAnalyseProjects/YoutubeVideoAnalysis/USvideos.csv")
# print(youtube)

# find first 5 data
firstFive = youtube.head(n=5)
# print(firstFive)

# delete the columns (thumbnail_link, video_id, trending_date, publish_time,comments_disabled,ratings_disabled,video_error_or_removed)
youtube.drop(["thumbnail_link", "video_id", "trending_date", "publish_time", "comments_disabled", "ratings_disabled","video_error_or_removed"], axis=1, inplace=True)
#print(youtube)

# Count columns and rows
## Rows
rows = len(youtube)
#print(rows)

## Columns
columns = len(youtube.columns)
#print(columns)

# find the mean of likes and dislike
## Likes
likes = youtube["likes"]
meanOfLikes = likes.mean()
#print(meanOfLikes)

## Dislikes
dislikes = youtube["dislikes"]
meanOfDislikes = dislikes.mean()
#print(meanOfDislikes)

# Find the most viewed videos title
mostViewed = youtube["views"].max()
#print(mostReviewed)
mostViewedTitle = youtube[(youtube["views"]==mostViewed)]["title"].iloc[0]
# print(mostViewedTitle)

# Find the less viewed videos title
lessViewed = youtube["views"].min()
# print(lessViewed)
lessViewedTitle = youtube[(youtube["views"] == lessViewed)]["title"].iloc[0]
# print(lessViewedTitle)

# Find the mean of comments by categories
groupByCategories = youtube.groupby("category_id")
# print(groupByCategories)
meanOfCommentsByCat = groupByCategories["comment_count"].mean()
# print(meanOfCommentsByCat)

# Find how many videos in each category
videosByCat = groupByCategories.count()["title"]
# print(videosByCat)

# Add a new Column which has the length of the title
## New Column title_length
youtube["title_length"] = youtube["title"].str.len()
#print(youtube)

# Find the tags count for every data and add as a column
def tagCount(tags=str):
    tagList=tags.split("|")
    return len(tagList)

youtube["tags_count"] = youtube["tags"].apply(tagCount)
# print(youtube)

# sort videos by likes/dislike in descending order
def ratioOfLikesDislikes(likes, dislikes):
    likesList = list()
    dislikesList = list()
    for key, value in likes.iteritems():
        likesList.append(value)
    for key, value in dislikes.iteritems():
        dislikesList.append(value)

    ratio = list()
    for like,dislike in zip(likesList,dislikesList):
        if like + dislike == 0:
            ratio.append(0)
        else:
            ratio.append(like/(like+dislike))
    return ratio

youtube["likes_ratio"] = ratioOfLikesDislikes(youtube["likes"], youtube["dislikes"])
youtube.sort_values("likes_ratio", ascending=False, inplace=True)
print(youtube)