Reputation: 177
I have an igraph graph object, g
, with the following list of attributes:
IN: g.vertex_attributes()
OUT: ['Label', 'mentioned', 'retweeted', 'community_id']
I then try to create a new attribute from an existing pandas dataframe as follows:
IN: g.vs['profile_status'] = y['profile_status']
... where y
looks like this:
IN: y.head()
OUT: user.screen_name profile_status
0 username1 User profile active
1 username2 User profile active
2 username3 Account deleted or suspended
3 username4 User profile active
4 username5 User profile active
...resulting in an attribute list that now looks like this with the new attribute added:
IN: g.vertex_attributes()
OUT: ['Label', 'mentioned', 'retweeted', 'community_id', 'profile_status']
However, the profile_status
field is not being written to the graphml file when I run this:
IN: g.write_graphmlz(NETWORK_OUTPATH)
Opening the graphml file in a text editor shows that there is no profile_status
field anywhere.
Any ideas what I am doing wrong? Thanks!
Upvotes: 0
Views: 168
Reputation: 177
igraph appears to have trouble with string attributes so converting the string values into integer codes did the trick. Here's how I did it:
# Generate list of statuses and their integer codes
possible_statuses = {y:x for x,y in enumerate(y['profile_status'].unique())}
print(possible_statuses)
y['profile_status_code'] = y['profile_status'].apply(lambda x: possible_statuses[x])
g.vs['profile_status_code'] = y['profile_status_code']
# Write network to file
g.write_graphmlz(NETWORK_OUTPATH)
Upvotes: 0