# ---- Pandas Python program that analyzes the Top 1000 Twitch streamers CSV data ----
import pandas as pd

data = pd.read_csv("twitchdata-update.csv")
df = pd.DataFrame(data)

line1 = "This .txt file generated by Python utilizes the Pandas library and analyzes the Top 1000 Twitch streamers of 2020 CSV file from my Project #1!"
line2 = "\n(Maximize the .txt file for optimized readability)\n"
line3 = "_____________________________________________________\n\n\n"

# The 1st ranked streamer on the list
top1 = df['Channel'][0]
top2 = (top1 + " is the top streamer on Twitch!\n\n")

# Streamer with the most followers
topFollowerCount = df['Followers'].max()
topFollowerCountStreamer = df.loc[df["Followers"] == topFollowerCount]["Channel"]

mostfollowers = topFollowerCountStreamer.values[0] + " is the streamer with the most followers on Twitch! " + topFollowerCountStreamer.values[0] + " has " + str(topFollowerCount) + " followers.\n\n"

# Finding the max peak viewer amount
peak = df['Peak_viewers'].max()
topPeakStreamer = df.loc[df["Peak_viewers"] == peak]["Channel"]
toppeak = topPeakStreamer.values[0] + " is the streamer who had the highest peak viewer count, at " + str(peak) + " viewers.\n\n"

# Streamer with the most followers gained
followg = df['Followers_gained'].max()
topFollowergStreamer = df.loc[df["Followers_gained"] == followg]["Channel"]
followsgained = topFollowergStreamer.values[0] + " gained the most followers on Twitch in 2020, with " + str(followg) + " followers gained.\n\n"

# Count of how many streamers in the list are partnered vs not partnered
partners = 0
for index, row in df.iterrows():
    if row['Partnered']:
        partners = partners + 1
nopartner = 1000 - partners

countpartners = "Of the Top 1000 streamers on Twitch, " + str(partners) + " are partnered on the platform.\n"
countnonpartners = str(nopartner) + " are NOT partnered on the platform.\n\n"

# Count of how many streamers have MATURE content vs don't
mature = 0
for index, row in df.iterrows():
    if row['Mature']:
        mature = mature + 1
nonmature = 1000 - mature

countmature = "Of the Top 1000 streamers on Twitch, " + str(mature) + " are considered MATURE content.\n"
countnonmature = str(nonmature) + " are considered non-mature content.\n\n"

# Count of how many streamers are English out of the total streamers
englishSpeaking = 0
for index, row in df.iterrows():
    if row['Language'] == "English":
        englishSpeaking = englishSpeaking + 1
nonenglishSpeaking = 1000 - englishSpeaking

countenglish = "Of the Top 1000 streamers on Twitch, " + str(englishSpeaking) + " are English speaking streamers.\n"
countnonenglish = str(nonenglishSpeaking) + " streamers are languages NOT English.\n\n"

# Sort streamers by total views gained
sort = df.sort_values('Views_gained', ascending=False)

sort_text = "Below is all the Top 1000 streamers on Twitch sorted by TOTAL VIEWS GAINED:\n"
sort_views = sort[['Channel', 'Views_gained']]  

# Sort streamers by total followers
sortedFollowers = df.sort_values('Followers', ascending=False)

sort2_text = "\n\nBelow is all the Top 1000 streamers on Twitch sorted by TOTAL FOLLOWERS on the platform:\n"
sort_followers = sortedFollowers[['Channel', 'Followers']]  

# Final thoughts
lastline1= "\n ________________________________________________________\n\n"
lastline2 = "Performing analysis of the Top 1000 Twitch Streamers CSV with Pandas made me learn how much easier it is to do analysis with the Pandas Library compared to standard syntax!"

# Writing all my findings into .txt file
with open('Top1000TwitchStreamers.txt','w') as out:
    out.writelines([line1,line2,line3,top2,mostfollowers,toppeak,followsgained,countpartners,countnonpartners,countmature,countnonmature,
    countenglish,countnonenglish,sort_text,sort_views.to_string(),sort2_text,sort_followers.to_string(), lastline1, lastline2])