{"id":18962990,"url":"https://github.com/plishka/python_visualization","last_synced_at":"2026-04-02T04:30:16.354Z","repository":{"id":244647122,"uuid":"815838105","full_name":"Plishka/python_visualization","owner":"Plishka","description":"Facebook Ad Campaigns Analysis and Visualization","archived":false,"fork":false,"pushed_at":"2024-06-16T10:59:33.000Z","size":1723,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-01T05:18:56.781Z","etag":null,"topics":["python","python-visualization"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Plishka.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-16T10:18:22.000Z","updated_at":"2024-06-16T11:05:13.000Z","dependencies_parsed_at":"2024-06-16T12:05:52.104Z","dependency_job_id":null,"html_url":"https://github.com/Plishka/python_visualization","commit_stats":null,"previous_names":["plishka/python_visualization"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Plishka%2Fpython_visualization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Plishka%2Fpython_visualization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Plishka%2Fpython_visualization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Plishka%2Fpython_visualization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Plishka","download_url":"https://codeload.github.com/Plishka/python_visualization/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239958310,"owners_count":19724926,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","python-visualization"],"created_at":"2024-11-08T14:17:29.706Z","updated_at":"2026-04-02T04:30:16.325Z","avatar_url":"https://github.com/Plishka.png","language":"Jupyter Notebook","readme":"# Facebook Ad Campaigns Analysis and Visualization\n\n## Overview\nThis project demonstrates a comprehensive process of analyzing and visualizing Facebook Ad Campaign data using Python and popular data science libraries such as **Pandas**, **Seaborn**, and **Matplotlib**. The analysis includes calculating moving averages, aggregating data, and identifying correlations to provide insights into advertising spend and Return on Marketing Investment (ROMI) for better decision-making and analysis.\n\n## Advanced Techniques and Approaches Used\n\n- **Data Cleaning**: Handling NaN values to ensure data integrity and accuracy in analysis.\n- **Rolling Averages**: Utilizing the `rolling()` method to calculate moving averages for smoothing time series data, providing clearer trends.\n- **Data Aggregation**: Grouping data by different categories to compute aggregate statistics, which simplifies complex data and highlights key trends.\n- **Visualization**: Creating various plots (line plots, bar plots, box plots, histograms, scatter plots) to visually represent data trends, distributions, and relationships.\n- **Heatmap Analysis**: Building a heatmap to visualize the correlation between different numerical indicators, aiding in the identification of key relationships.\n\n## Conclusion\n\nThis analysis of Facebook Ad Campaigns provided valuable insights into advertising spend and ROMI across different campaigns. Key findings include:\n\n- The rolling average analysis highlighted fluctuations in daily advertising spend and ROMI throughout 2021.\n- The total spend and ROMI analysis per campaign revealed which campaigns were the most cost-effective and yielded the highest returns.\n- The distribution of daily ROMI and the overall ROMI distribution provided a comprehensive view of performance variability across campaigns.\n- Correlation analysis identified strong relationships between various metrics, particularly between total spend and total value, aiding in a better understanding of what drives value.\n\nThese insights can guide future ad spending decisions to optimize return on investment.\n\n## Python Code\n\n```python\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\n\nplt.rcParams['figure.figsize'] = [30, 10]  # setting standard plot size for all notebook\nsns.set(style=\"whitegrid\")  # general style of the plots\n\n# Load data\ncampaigns = pd.read_csv('C:/Users/plish/Desktop/Python_HW/Facebook Ad campaigns Analysis and Viz/facebook_ads_data.csv')\n\n# Cleaning data from NaN values (strings)\ncampaigns.dropna(subset=['cpc', 'ctr', 'romi'], inplace=True)\ncampaigns.info()  # data structure check\n\n# Create grouped data set for the year 2021\ndata_2021 = campaigns.loc[campaigns['ad_date'].between('2021-01-01', '2021-12-31')]\ndata_set = data_2021.groupby('ad_date').agg({'total_spend': 'sum', 'romi': 'mean'})\n\n# Daily rolling average Ad Spend in 2021 Chart\nsns.lineplot(x='ad_date', y=data_2021['total_spend'].rolling(10).mean(), data=data_2021, color='orange')\nplt.title('Daily Rolling Average Ad Spend in 2021', fontdict={'fontsize': 30, 'fontweight': 'bold'}, pad=20)\nplt.xlabel('Date', fontsize='xx-large')\nplt.ylabel('Ad Spend, $', fontsize='xx-large')\nplt.xticks(rotation=90)\nplt.xticks(list(campaigns['ad_date'])[1::30])\nplt.show()\n\n# Daily ROMI in 2021 Chart\nsns.lineplot(x='ad_date', y=data_2021['romi'].rolling(10).mean(), data=data_2021, color='green')\nplt.title('Daily Rolling Average ROMI in 2021', fontdict={'fontsize': 30, 'fontweight': 'bold'}, pad=20)\nplt.xlabel('Date', fontsize='xx-large')\nplt.ylabel('ROMI', fontsize='xx-large')\nplt.xticks(rotation=90)\nplt.xticks(list(campaigns['ad_date'])[1::30])\nplt.show()\n\n# Creating data frame aggregated by ad campaign\ncampaign_data = campaigns.groupby('campaign_name').agg({'total_spend': 'sum', 'romi': 'mean'}).reset_index()\n\n# Total Spend on Ad Campaign\nax = sns.barplot(x='campaign_name', y='total_spend', data=campaign_data)\nplt.title('Total Ad Spend by Campaigns', fontdict={'fontsize': 30, 'fontweight': 'bold'}, pad=20)\nplt.xlabel(None, fontsize='xx-large')\nplt.ylabel('Total Spend', fontsize='xx-large')\n\n# Display values on each bar\nfor p in ax.patches:\n    ax.annotate(f'{p.get_height():.0f}', (p.get_x() + p.get_width() / 2., p.get_height()), ha='center', va='center', fontsize=16, color='black', xytext=(0, 10), textcoords='offset points')\nplt.show()\n\n# Total ROMI on each Campaign\nax = sns.barplot(x='campaign_name', y='romi', data=campaign_data)\nplt.title('ROMI by Campaigns', fontdict={'fontsize': 30, 'fontweight': 'bold'}, pad=20)\nplt.xlabel(None, fontsize='xx-large')\nplt.ylabel('ROMI', fontsize='xx-large')\n\n# Display values on each bar\nfor p in ax.patches:\n    ax.annotate(f'{(p.get_height())*100:.2f}%', (p.get_x() + p.get_width() / 2., p.get_height()), ha='center', va='center', fontsize=16, color='black', xytext=(0, 10), textcoords='offset points')\nax.tick_params(axis='x', which='major', labelsize=16)\nplt.show()\n\n# Determine the daily ROMI spread in each campaign (by campaign name) using a box plot\nfrom matplotlib.ticker import PercentFormatter  # import to be able to format Y-axis to %\ncampaigns['romi_perc'] = campaigns['romi'] * 100  # create new column to convert ROMI to %\ndaily_romi = sns.boxplot(x='campaign_name', y='romi_perc', data=campaigns)\nplt.title('Distribution of Daily ROMI by Campaign', fontdict={'fontsize': 30, 'fontweight': 'bold'}, pad=20)\nplt.xlabel(None, fontsize='xx-large')\nplt.ylabel('ROMI (%)', fontsize='xx-large')\ndaily_romi.set_ylim(0, 275)  # set y-axis limits\ndaily_romi.yaxis.set_major_formatter(PercentFormatter(decimals=0))\ndaily_romi.tick_params(axis='x', which='major', labelsize=16)\nplt.show()\n\n# Create histogram to show the distribution of ROMI values in the data set table.\nhist_plot = sns.histplot(campaigns['romi_perc'], bins=30, color='skyblue', edgecolor='black', kde=True)\nplt.title('Histogram of ROMI Values Distribution', fontdict={'fontsize': 30, 'fontweight': 'bold'}, pad=20)\nplt.xlabel('ROMI (%)', fontsize='xx-large')\nplt.ylabel('Frequency', fontsize='xx-large')\nhist_plot.tick_params(axis='both', which='major', labelsize=16)\nplt.xticks(range(0, int(campaigns['romi_perc'].max()) + 50, 10))  # set x-axis tick step\nhist_plot.set_xlim(0, 270)  # set x-axis limits\nhist_plot.set_ylim(0, 130)  # set y-axis limits\nplt.show()\n\n# Build a heatmap of the correlation\ndf = campaigns.iloc[:, 2:9]  # get data frame for the chart\nplt.figure(figsize=(30, 12))\nsns.heatmap(df.corr(), annot=True, cmap='coolwarm', xticklabels=['Total Spend', 'Total Impressions', 'Total Clicks', 'Total Value', 'CPC', 'CPM', 'CTR'], yticklabels=['Total Spend', 'Total Impressions', 'Total Clicks', 'Total Value', 'CPC', 'CPM', 'CTR'], linewidth=1)\nplt.title('Сorrelation Heatmap', fontdict={'fontsize': 20, 'fontweight': 'bold'}, pad=10)\nplt.show()\n\n# What is \"total_value\" correlated with?\ncorrelation_matrix = df.corr()\n# Drop self-correlation\ntotal_value_corr = correlation_matrix['total_value'].drop('total_value')\nprint(f'Correlation of \"Total value\" with other indicators:\\n{round(total_value_corr,2)}')\n\n# Which indicators have the highest and lowest correlation?\n# Get the highest correlation pairs\nhighest_corr = correlation_matrix.unstack().sort_values().drop_duplicates().tail(2)\n# Get the lowest correlation pairs\nlowest_corr = correlation_matrix.unstack().sort_values().drop_duplicates().head(2)\nprint(f'Highest correlation:\\n{highest_corr}\\n')\nprint(f'Lowest correlation:\\n{lowest_corr}')\n\n# Create a scatter plot with linear regression\nsns.lmplot(x='total_spend', y='total_value', data=campaigns)\nplt.title('Scatter plot with a Linear Regression Line', fontdict={'fontsize': 15, 'fontweight': 'bold'}, pad=20)\nplt.xlabel('Total Spend', fontsize='medium')\nplt.ylabel('Total Value', fontsize='medium')\nhist_plot.tick_params(axis='both', which='major', labelsize=10)\nhist_plot.set_xlim(0, 2500)  # set x-axis limits\nhist_plot.set_ylim(0, 3000)  # set y-axis limits\nplt.show()\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplishka%2Fpython_visualization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplishka%2Fpython_visualization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplishka%2Fpython_visualization/lists"}