df = pd.read_csv("congress.csv", error_bad_lines=False)

"""
Explanation: Open your dataset up using pandas in a Jupyter notebook
End of explanation
"""


df.head()

#bioguide: The alphanumeric ID for legislators in http://bioguide.congress.gov.

"""
Explanation: Do a .head() to get a feel for your data
End of explanation
"""


df['chamber'].value_counts() #sounds like a lot. We might have repetitions.

df['bioguide'].describe() #we count the bioguide, which is unique to each legislator.
#There are only 3188 unique values, hence only 3188 senators and representatives in total.

"""
Explanation: Write down 12 questions to ask your data, or 12 things to hunt for in the data
1)How many senators and how many representatives in total since 1947?
End of explanation
"""


total_democrats = (df['party'] == 'D').value_counts()
total_democrats

total_republicans =(df['party'] == 'R').value_counts()
total_republicans

"""
Explanation: 2) How many from each party in total ?
End of explanation
"""


df['age'].describe()

"""
Explanation: 3) What is the average age for people that have worked in congress (both Senators and Representatives)
End of explanation
"""


df.groupby("chamber")['age'].describe()

"""
Explanation: 4) What is the average age of Senators that have worked in the Senate? And for Representatives in the house?
End of explanation
"""


df['state'].value_counts()

"""
Explanation: 5) How many in total from each state?
End of explanation
"""


df.groupby("state")['chamber'].value_counts()

"""
Explanation: 5) How many Senators in total from each state? How many Representatives?
End of explanation
"""


df['termstart'].describe() #here we would look at unique.

"""
Explanation: 6) How many terms are recorded in this dataset?
End of explanation
"""


df.sort_values(by='age').tail(1) #A senator!

"""
Explanation: 7) Who has been the oldest serving in the US, a senator or a representative? How old was he/she?
End of explanation
"""


representative = df[df['chamber'] == 'house']
representative.sort_values(by='age').tail(1)

representative.sort_values(by='age').head(2)

"""
Explanation: 8) Who have been the oldest and youngest serving Representative in the US?
End of explanation
"""


senator = df[df['chamber'] == 'senate']
senator.sort_values(by='age')

senator.sort_values(by='age').head(2)

"""
Explanation: 9) Who have been the oldest and youngest serving Senator in the US?
End of explanation
"""


# Store a new column
df['complete_name'] = df['firstname']+ " "+ df['middlename'] + " "+df['lastname']
df.head()

period_count = df.groupby('complete_name')['termstart'].value_counts().sort_values(ascending=False)
pd.DataFrame(period_count)


#With the help of Stephan we figured out that term-start is every 2 years 
#(so this is not giving us info about how many terms has each legislator served)


"""
Explanation: 10) Who has served for more periods (in this question I am not paying attention to the period length)?
End of explanation
"""


terms_served_by_senators= senator.groupby('complete_name')['bioguide'].value_counts()
years= terms_served_by_senators * 2
total_years_served = years.sort_values(ascending=False)

pd.DataFrame(total_years_served)





terms_served_by_representative= representative.groupby("complete_name")['bioguide'].value_counts()
years= terms_served_by_representative * 2
total_years_served = years.sort_values(ascending=False)

pd.DataFrame(total_years_served)




"""
Explanation: We double-checked it by printing info from Thurmond, whom was part of the senate but appeared as if he had
served 26 periods of 6 years each (26*6 IMPOSIBLE!)
Thurmond = df[df['lastname'] == 'Thurmond']
Thurmond
11) Who has served for more years?
Senators = 6-year terms BUT the data we have is for 2-year terms
Representatives = 2-year terms
End of explanation
"""


df['firstname'].value_counts()

#this might be counting the same person many times but still we can get an idea of what names are more popular

"""
Explanation: 12)The most popular name in congress is....
End of explanation
"""


plt.style.use("ggplot")
df['age'].hist(bins=15, xlabelsize=12, ylabelsize=12, color=['y'])

df.head(20).sort_values(by='age',ascending=True).plot(kind='barh', x=['complete_name'], y='age', color="y")

df.plot.scatter(x='congress', y='age');

df.plot.hexbin(x='age', y='congress', gridsize=25, legend=True)

"""
Explanation: Make three charts with your dataset
1) Distribution of age
End of explanation
"""


