<jupyter_start><jupyter_text>Politician Activity on Twitter by Political AffiliationThe parameters in the cell below can be adjusted to explore other political affiliations and time frames. How to explore other political affiliation?The ***affiliation*** parameter can be use to aggregate politicians by their political affiliations. The column `affiliation` in this [this other notebook](../politicians.ipynb?autorun=true) show the politicians that belong each political affiliation.***Alternatively***, you can direcly use the [politicians API](http://mediamonitoring.gesis.org/api/politicians/swagger/), or access it with the [SMM Wrapper](https://pypi.org/project/smm-wrapper/). A. Set Up parameters<jupyter_code># Parameters: 
affiliation = 'CSU'
from_date = '2017-09-01'
to_date = '2018-12-31'
aggregation = 'week'<jupyter_output><empty_output><jupyter_text>B. Using the SMM Politician API<jupyter_code>import pandas as pd

# create an instance to the smm wrapper
from smm_wrapper import SMMPoliticians
smm = SMMPoliticians()

# using the api to get the data
df = smm.dv.get_politicians()

# Filter the accounts by party, and valid ones (the ones that contain tw_ids)
party_df = df[(df['affiliation']==affiliation) & (df['tw_ids'].notnull())] 

# query the Social Media Monitoring API
tweets_by = pd.concat(smm.dv.tweets_by(_id=politician_id, from_date=from_date, to_date=to_date, aggregate_by=aggregation) 
               for politician_id in party_df.index)
replies_to = pd.concat(smm.dv.replies_to(_id=politician_id, from_date=from_date, to_date=to_date, aggregate_by=aggregation) 
               for politician_id in party_df.index)

# aggregate the replies
total_tweets_by = tweets_by.groupby('date').agg({'tweets': 'sum'})
total_replies_to = replies_to.groupby('date').agg({'replies': 'sum'})<jupyter_output><empty_output><jupyter_text>C. Plotting<jupyter_code>#plotting data
import plotly
from plotly import graph_objs as go
plotly.offline.init_notebook_mode(connected=True)

plotly.offline.iplot({
    "data": [go.Scatter(x=total_tweets_by.index, y=total_tweets_by['tweets'], name='Tweets', line_shape='spline'), 
             go.Scatter(x=total_replies_to.index, y=total_replies_to['replies'], name='Replies', line_shape='spline')], 
    "layout": go.Layout( title='Twitter (Tweets and Replies)', yaxis=dict(title='N'))
})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11787072243346007, 'fraction_numerical': 0.006759611322348965, 'mean_word_length': 6.263803680981595, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '491516', 'license': 'MIT', 'n_tokens_mistral': 810, 'n_tokens_neox': 786, 'n_words': 192, 'path': 'python/affiliations/twitter.ipynb', 'repo_name': 'gesiscss/smm_demo', 'size': '2367'}