<jupyter_start><jupyter_text>Course Prerequisites Structure Using Sankey Here, I am going to show flow from course to its prerequisites.Notice that, I have made 1 change in File. I have filled "Passrate" with values which will be work as **value** for sankey diagram.<jupyter_code>import plotly
import pandas as pd
import numpy as np
from plotly.graph_objs import Sankey
import random
data = pd.read_csv('FDU Electrical Engineering.csv', sep =",")
print(type(data))
print("Shape will show number of rows and columns in DataFrame (rows,columns):",data.shape)
data.head()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Shape will show number of rows and columns in DataFrame (rows,columns): (44, 6)<jupyter_text>Creating mapping for source and target. * In our case, column "Name" will work as source * column "Prerequisites" will work as target* and column "Passrate" will work as value ** I have already explained process of mapping that is created for source and target with replacing those mapping in our DataFrame.** What would be different here ?1. Here I am going to replace "NaN" with value 0 (which will act as very first node.)2. I will assign name to that node (which will be very first value in labels list.)3. The reason behind that is to show all the subjects from each term. In sankey we can not show nodes without target and values. It is must to have target and value. (If value would be 0 then that connection won't be shown at all.)<jupyter_code>source_name = data['Name'].tolist()
print(len(source_name))
source_name[:6]
source_zip = list(range(1,45))
source_mapping = dict(zip(source_name, source_zip))
source_mapping<jupyter_output><empty_output><jupyter_text>Creating copy of "data" DataFrame to perform mapping<jupyter_code>df = data.copy()
df['Name'].replace(source_mapping, inplace = True)
df[-10:]
pre_requisites = data['Prerequisites'].dropna().unique().tolist()
print(len(pre_requisites))
pre_requisites[:5]
target_zip = list(range(45,66))
target_mapping = dict(zip(pre_requisites, target_zip))
target_mapping<jupyter_output><empty_output><jupyter_text>Replacing "NaN" with 0<jupyter_code>df.replace(np.nan, 0, inplace=True)
df['Prerequisites'].replace(target_mapping, inplace = True)
df[:5]<jupyter_output><empty_output><jupyter_text>Creating labels and giving name to very first node which will represent all NaN values.<jupyter_code>labels = ['No Pre_req']
labels = labels + source_name + pre_requisites<jupyter_output><empty_output><jupyter_text>Creating sankey<jupyter_code>from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from plotly.graph_objs import *

init_notebook_mode(connected=True)
data_trace = dict(
    type='sankey',
    domain = dict(
      x =  [0,1],
      y =  [0,1]
    ),
    orientation = "h",
    visible = True,
    valueformat = ".0f",
    valuesuffix = " Students",
    node = dict(
      pad = 15,
      thickness = 30,
      line = dict(
        color = "black",
        width = 0.5
      ),
      label =  labels,
      #color = df['node_color']
    ),
    link = dict(  
      source = df['Name'],
      target = df['Prerequisites'],
      value = df['Passrate'],
      #color = df['link_color'],# Here we can add colors for each link which connected source to target.
  )
)

layout =  dict(
    title = "Course Prerequisite Structure For BS Electrical Engineering",
    height = 1000,
    width = 1000,
    font = dict(
      size = 12
    ),    
)
fig = Figure(data=[data_trace], layout=layout)
plotly.offline.plot(fig, validate=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10586913788261725, 'fraction_numerical': 0.012356079752878404, 'mean_word_length': 4.636075949367089, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '428189', 'license': 'MIT', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1093, 'n_words': 369, 'path': '.ipynb_checkpoints/Course Pre_req Flow for BS Electrical Engineering-checkpoint.ipynb', 'repo_name': 'HemangLakhani/StudentDataAnalysis', 'size': '3561'}