import numpy as np
import pandas as pd
import networkx as nx
import json
import sys

batchlogfile = 'sample_dataset/batch_log.json'
df_batch = pd.read_json(batchlogfile, lines=True)

df_purchase = df_batch[df_batch['event_type']=='purchase']
df_purchase = df_purchase[['event_type','id','timestamp','amount']]
# If sort on the timestamp is needed, commentout the following line
# df_purchase = df_purchase.sort_values('timestamp')
#df_purchase.shape

df_friend=df_batch[(df_batch['event_type']=='befriend') | (df_batch['event_type']=='unfriend')]
df_friend=df_friend[['event_type','id1','id2','timestamp']]
# If sort on the timestamp is needed, commentout the following line
#df_friend=df_friend.sort_values('timestamp')
#df_friend.shape

G = nx.Graph()

id1list = df_friend.id1.tolist()
id2list = df_friend.id2.tolist()
id3list = df_purchase.id.tolist()
idlist = set(id1list + id2list + id3list)
G.add_nodes_from(idlist)
#len(list(G.nodes()))

def Add_edges(data):
    for row in data.itertuples():
        id10 = row.id1
        id20 = row.id2
        event_type0 = row.event_type
        if event_type0 == 'befriend':
            G.add_edge(id10,id20)
        if event_type0 == 'unfriend':
            if G.has_edge(id10,id20):
                G.remove_edge(id10,id20)  
								
def test_Add_edges():
		Add_edges(df_friend)
	

@@ -176,16 +176,129 @@
 =True)%0A%0A
+index_purchase = %5B'event_type','id','timestamp','amount'%5D%0Aindex_friend = %5B'event_type','id1','id2','timestamp'%5D%0A%0A
 df_purch
@@ -378,49 +378,67 @@
 ase%5B
-%5B'event_type','id','timestamp','amount'%5D%5D
+index_purchase%5D%0Adf_purchase = df_purchase.dropna(how='any')
 %0A# I
@@ -584,17 +584,19 @@
 f_friend
-=
+ = 
 df_batch
@@ -674,33 +674,35 @@
 end')%5D%0Adf_friend
-=
+ = 
 df_friend%5B%5B'even
@@ -699,47 +699,62 @@
 end%5B
-%5B'event_type','id1','id2','timestamp'%5D%5D
+index_friend%5D%0Adf_friend = df_friend.dropna(how='any') 
 %0A# I
@@ -902,85 +902,40 @@
 ()%0A%0A
-id1list = df_friend.id1.tolist()%0Aid2list = df_friend.id2.tolist()%0A
+def Add_nodes(data):%0A%09
 id
-3
 list = d
 f_pu
@@ -930,26 +930,19 @@
 list = d
-f_purchase
+ata
 .id.toli
@@ -946,16 +946,17 @@
 olist()%0A
+%09
 idlist =
@@ -966,35 +966,15 @@
 t(id
-1list + id2list + id3
 list)%0A
+%09
 G.ad
@@ -994,16 +994,17 @@
 idlist)%0A
+%09
 #len(lis
@@ -1018,16 +1018,18 @@
 es()))%0A%0A
+%0A%0A
 def Add_
@@ -1380,17 +1380,16 @@
 dges():%0A
-%09
 %09Add_edg
@@ -1404,8 +1404,57 @@
 iend)%0A%09%0A
+%0A%0Adef test_Add_nodes():%0A%09Add_nodes(df_purchase) %0A

update test_Add_edges.py