Created
February 4, 2019 21:53
-
-
Save talfco/06a99482355b2217d830c3cc3da2f600 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_party_table(self): | |
# Panda Data Frame - Table of Tweeter Users per Party | |
# https://stackoverflow.com/questions/48909110/python-pandas-mean-and-sum-groupby-on-different-columns-at-the-same-time | |
panda_data = { "Party": self.__col_party, | |
"PartyCount": self.__col_party, # Duplicate Party column for the counting | |
"FollowersCount": self.__col_followers_count, | |
"FriendsCount": self.__col_friends_count | |
} | |
df = DataFrame(panda_data , index=self.__labels) | |
# We use the as_index parameter, so that Party will also be a column of the data frame | |
df = df.groupby(["Party"], as_index=False).agg({'PartyCount':'count','FollowersCount':'sum', 'FriendsCount': 'sum'}) | |
df = df.sort_values(['FollowersCount'], ascending=[0]) | |
print(df) | |
# Create a Plotly Table | |
trace = go.Table( | |
header = dict(values=list(df.columns)), | |
cells = dict(values=[df.Party, df.PartyCount, df.FollowersCount, df.FriendsCount])) | |
data = [trace] | |
py.plot(data, filename=self.__country_code+'-tw-party-list') | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment