Analysing my Facebook data using Python
By: Olamide Olowoniyi
Tags: Facebook, Data Analytics, Data, Data Science, Social Media

Photo by NeONBRAND on Unsplash

I saw this tutorial on social media data analysis and found it really interesting. So, I decided to also analyse my Facebook usage since I joined in 2010. See how I did it here:

1. Downloading my Facebook Data:

Facebook gives everyone the permission the download data of all their activity on the site and the data can be downloaded here. The downloaded file can be massive depending on how long you have been on the site or how heavy of a user you are. My file was 8 MB. You can also choose the type of data to download from file depending on what you are looking to analyse. I chose to focus only on my Facebook posts.

I downloaded only the data related to my posts from the data download page by deselecting everything from the options and checking just the ‘posts’ option. I changed the requested file format from HTML to JSON, and hit Create File. After the file was created, I got a notification from Facebook (depending on the history and options selected, this process may take a while). I downloaded the zip file and extracted the your_posts_1.json file.

2. Cleaning and Formatting the Downloaded Data

This was where the real work began! For this analysis, I was looking to answer two major questions:

How often are my Facebook posts If there has been a change in the frequency of my posts

To start, I imported all the relevant python libraries I needed for my analysis using the Jupiter Notebook. I also imported and read my data into a pandas data frame using the built-in pd.read_json() function.

The results generated from running this function needed some cleaning because my focus was on the frequency of the post, not the attached files or the content of the post. So I dropped all the unnecessary columns.

df.shape provided information on the number of rows and columns I was analysing, therefore indicating that I had a total of 465 posts on Facebook since 2010. That is not so much now. LOL.

Having dropped all the unnecessary columns, I renamed the timestamp column to date column. The data looked tidier at this point.

3. Counting my monthly posts:

It was better to analyse the dates in the data monthly and to do this, I grouped the date column into months and counted how many posts were associated with each month. Working with the Pandas library made this easier to do. I set the date column as the index of the data frame using the set_index() function. Then I resampled the data by month using the .resample() function with the argument ‘MS’ (for “Month Start”). The .size() function was used to specify each month (the number of rows or posts with a post date that falls within that particular month). The resulting series was then assigned to a variable referred to as the post_counts.

Satisfied with how this looked; the post_counts broken down by months. I proceeded to visualise my data.

4. Visualising my Facebook data

I had already imported matplotlib and seaborn to make my chart appear in the Jupyter notebook and the latter to make the chart easily readable. I used the sns.set() function to set the size and font size of my chart and the function to create the bar chart. I arranged the tick positions on the x-axis of the bar chart to once every 12 months, such that the resultant chart shows a tick every year.

And there you have it folks! My Facebook usage data. This chart shows that I was most active in 2014 which is very interesting as that year was my final year in university, so how come o? Shouldn’t I have been facing my book then? LOL. In 2019, I didn’t post at all. I started posting again 2020, I remember I have been sharing some of my writing on Facebook this year again.

Well, would you like to yours next?

Reference: https://www.dataquest.io/blog/analyze-facebook-data-python/