<jupyter_start><jupyter_text>Try Directional Graph DB* Download the data from https://cdn.analyticsvidhya.com/wp-content/uploads/2020/01/ipl_batting_partnerships.csv<jupyter_code>import pandas as pd
import numpy as np
import networkx as nx
from tqdm import tqdm
import warnings

import matplotlib.pyplot as plt
df = pd.read_csv("ipl_batting_partnerships.csv", index_col='index')
print(df.shape)
df.head()<jupyter_output>(424, 5)<jupyter_text>Build Graph DB* Calculate median score of each player in a partnership* In a partnership, the player contributed higher percentage of the score is the lead, and in the graph, the arrow should point to the lead in a relationship<jupyter_code>partner_df = df[['player_1', 'player_2']].drop_duplicates()
print(partner_df.shape)

partner_df.head()
play1_median_score_df = df[['player_1', 'score_1']].groupby(['player_1'],
                                                    as_index=False)['score_1']\
                        .agg(['median']).reset_index()

play1_median_score_df = play1_median_score_df.rename(index=str, columns={'median': 'median_score_1'})
print(play1_median_score_df.shape)
play1_median_score_df.head()
play2_median_score_df = df[['player_2', 'score_2']].groupby(['player_2'],
                                                    as_index=False)['score_2']\
                        .agg(['median']).reset_index()

play2_median_score_df = play2_median_score_df.rename(index=str, 
                                        columns={'median': 'median_score_2'})
print(play2_median_score_df.shape)
play2_median_score_df.head()
partner_df = partner_df.merge(play1_median_score_df, on='player_1', how='left')
print(partner_df.shape)

partner_df.head()
partner_df = partner_df.merge(play2_median_score_df, on='player_2', how='left')
print(partner_df.shape)

partner_df.head()
partner_df['lead'] = np.where(partner_df['median_score_1'] >= partner_df['median_score_2'], 
                              partner_df['player_1'], partner_df['player_2'])
partner_df['follower'] = np.where(partner_df['lead']==partner_df['player_1'], 
                                  partner_df['player_2'], partner_df['player_1'])
partner_df['higher_score'] = np.where(partner_df['median_score_1'] > partner_df['median_score_2'],
                                      partner_df['median_score_1'], 
                                      partner_df['median_score_2'])
partner_df['total_score'] = partner_df['median_score_1'] + partner_df['median_score_2']
partner_df['performance'] = partner_df['higher_score']/(partner_df['total_score'] + 0.001)

partner_df.head()
G = nx.from_pandas_edgelist(partner_df, "follower", "lead", ['performance'], 
                            create_using=nx.MultiDiGraph())
plt.figure(figsize=(9,9))
pos = nx.spring_layout(G, k = 100, seed = 10) # k regulates the distance between nodes
nx.draw(G, with_labels=True, node_color='green', node_size=50, pos = pos,
        edgelist=G.edges(), edge_color='purple', arrowsize=15, alpha=0.6)
plt.show()
# get the edge weights
weights = list(nx.get_edge_attributes(G, 'performance').values())
print(weights[0:4])
for u, v, data in G.in_edges(data=True):
  print(u,v,data)
  break
for u, v, data in G.out_edges(data=True):
  print(u,v,data)
  break
# find the partnership with the highest weight
partnership_dct = nx.get_edge_attributes(G, 'performance')
sorted_partnership = {k: v for k, v in sorted(partnership_dct.items(),
                                              key=lambda item: item[1], 
                                              reverse=True)}
print(sorted_partnership)
for u, v, data in G.in_edges(data=True):
  if data['performance']>=0.999923:
    print(u,v,data)
    break

for u, v, data in G.out_edges(data=True):
  if data['performance']>=0.999923:
    print(u,v,data)
    break
G.degree
G.in_degree
G.out_degree<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1320901320901321, 'fraction_numerical': 0.0214970214970215, 'mean_word_length': 3.653012048192771, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '152925', 'license': 'MIT', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1284, 'n_words': 234, 'path': 'directional_graphDB.ipynb', 'repo_name': 'hanhanwu/Hanhan_COLAB_Experiemnts', 'size': '3861'}