Skip to content

Instantly share code, notes, and snippets.

@talfco
Created February 4, 2019 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talfco/06a99482355b2217d830c3cc3da2f600 to your computer and use it in GitHub Desktop.
Save talfco/06a99482355b2217d830c3cc3da2f600 to your computer and use it in GitHub Desktop.
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