Rize S. answered 03/23/23
Senior IT Certified Trainer, IT Developer & DBA Administrator
Import required packages
import pandas as pd import networkx as nx
Mount the drive to access the annotations.csv file
from google.colab import drive drive.mount('/content/gdrive')
Define the file path
filepath = '/content/gdrive/MyDrive/annotations.csv'
Load the csv file into pandas dataframe
df = pd.read_csv(filepath)
Extract hashtags from the tweets and create a list
hashtags = [] for text in df['text']: hashtag_list = [tag.strip("#") for tag in text.split() if tag.startswith("#")] hashtags.extend(hashtag_list)
Create a co-occurrence network of hashtags
graph = nx.Graph() for i in range(len(hashtags)): for j in range(i+1, len(hashtags)): if hashtags[i] != hashtags[j]: if graph.has_edge(hashtags[i],hashtags[j]): graph[hashtags[i]][hashtags[j]]['weight'] += 1 else: graph.add_edge(hashtags[i],hashtags[j], weight=1)
Print number of nodes and edges
print("Number of nodes:", len(graph.nodes)) print("Number of edges:", len(graph.edges))