Skip to content

Instantly share code, notes, and snippets.

View Ze1598's full-sized avatar

José Fernando Costa Ze1598

  • Porto, Portugal
View GitHub Profile
@Ze1598
Ze1598 / bin_integers.py
Last active May 4, 2020 15:26
Bin data using custom intervals with pandas (integers)
import pandas as pd
import numpy as np
import plotly.express as px
from typing import List, Union
def bin_ages(age_series: pd.Series) -> pd.Series:
age_labels = [f"[{i}, {i+10})" for i in range(0, 91, 10)]
age_bins = pd.IntervalIndex.from_tuples(
@Ze1598
Ze1598 / plotly_plot.py
Created April 28, 2020 12:13
Unpivot data with delimiters (plotly visualization)
import pandas as pd
import plotly.graph_objects as go
# Load the resulting dataset
data = pd.read_csv("final_data.csv")
# Group by social networks AND gender
gender_socials = data.groupby(by=["Used Social Networks", "Gender"]).count()
# Unstack the genders, leaving only the social networks on the index\
@Ze1598
Ze1598 / matplotlib_plot.py
Created April 28, 2020 12:13
Unpivot data with delimiters (matplotlib visualization)
import pandas as pd
from matplotlib import pyplot as plt
# Load the resulting dataset
data = pd.read_csv("final_data.csv")
# Group by social networks AND gender
gender_socials = data.groupby(by=["Used Social Networks", "Gender"]).count()
# Unstack the grouped DataFrame and plot it in a bar chart (the genders\
@Ze1598
Ze1598 / unpivot_delimited_data_v2.py
Last active April 27, 2020 21:50
Unpivot data with delimiters (v2)
import pandas as pd
from typing import List
# Base clode for splitting and unpivoting a Series
# https://stackoverflow.com/questions/19482970/get-list-from-pandas-dataframe-column-headers
def unpivot_delimited_series(
series: pd.Series,
delimiter: str
) -> pd.DataFrame:
@Ze1598
Ze1598 / unpivot_delimited_data.py
Last active April 27, 2020 21:49
Unpivot data with delimiters (v1)
import pandas as pd
from typing import List
# Base clode for splitting and unpivoting a Series
# https://stackoverflow.com/questions/19482970/get-list-from-pandas-dataframe-column-headers
def unpivot_delimited_series(
target_series: pd.Series,
delimiter: str
) -> pd.DataFrame:
@Ze1598
Ze1598 / artist_frequencies_map_reduce.py
Last active April 22, 2020 16:31
Spotify - count artist frequencies with map reduce
from functools import reduce
# Refactor of this function -> https://gist.github.com/Ze1598/cd32d9867dac11a029937539bac91721#file-get_spotify_playlist_data-py-L58-L73
def get_artist_counts(artists_info: List[List[Dict]]) -> Dict[str, int]:
"""
Find the frequency of each artist featured in the playlist and return
a dictionary of the type Artist:Frequency.
"""
def map_fn(track_artists: List[Dict]) -> List[str]:
"""
@Ze1598
Ze1598 / spotify_track_artists_data.json
Last active April 21, 2020 19:25
Spotify Track Artists Information
[
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/6kNKUYGn6VNGsRoXmyoDPK"
},
"href": "https://api.spotify.com/v1/artists/6kNKUYGn6VNGsRoXmyoDPK",
"id": "6kNKUYGn6VNGsRoXmyoDPK",
"name": "The Amity Affliction",
"type": "artist",
"uri": "spotify:artist:6kNKUYGn6VNGsRoXmyoDPK"
@Ze1598
Ze1598 / sample_track_data.json
Last active April 22, 2020 16:11
Spotify Track Information
{
"album": {
"album_type": "album",
"(...)"
},
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/6kNKUYGn6VNGsRoXmyoDPK"
},
@Ze1598
Ze1598 / analyze_spotify_playlist_data.py
Created April 21, 2020 18:13
Plot Spotify playlist data
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
# Load the data and sort it by artist frequency in descending order
def pre_process_data(file_name: str) -> pd.DataFrame:
dataset = pd.read_csv(file_name)
# Sort the dataset by "Frequency", in descending order, and reset the indices
dataset = dataset.sort_values(
@Ze1598
Ze1598 / get_spotify_playlist_data.py
Last active October 24, 2020 02:51
Get Spotify playlist data
from os import environ
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import pandas as pd
from typing import List, Dict
# https://github.com/plamere/spotipy/blob/master/examples/playlist_tracks.py
# Authenticate to Spotify
def authenticate(cliend_id: str, client_secret: str) -> spotipy.client.Spotify: