问题描述
def create_network(df,node,column_edge,column_edge1=None,column_edge2=None):
# select columns,remove NaN
df_edge1 = df[[node,column_edge]].dropna(subset=[column_edge]).drop_duplicates()
# To create connections between "node" who have the same "edge",# join data with itself on the "node" column.
df_edge1 = df_edge1.merge(
df_edge1[[node,column_edge]].rename(columns={node:node+"_2"}),on=column_edge
)
# By joining the data with itself,node will have a connection with themselves.
# Remove self connections,to keep only connected nodes which are different.
edge1 = df_edge1[~(df_edge1[node]==df_edge1[node+"_2"])].dropna()[[node,node +"_2",column_edge]]
# To avoid counting twice the connections (person 1 connected to person 2 and person 2 connected to person 1)
# we force the first ID to be "lower" then ID_2
edge1.drop(edge1.loc[edge1[node+"_2"]<edge1[node]].index.tolist(),inplace=True)
G = nx.from_pandas_edgelist(df=edge1,source=node,target=node + '_2',edge_attr=column_edge)
G.add_nodes_from(nodes_for_adding=df[node].tolist())
if column_edge1:
df_edge2 = df[[node,column_edge1]].dropna(subset=[column_edge1]).drop_duplicates()
df_edge2 = df_edge2.merge(
df_edge2[[node,column_edge1]].rename(columns={node:node+"_2"}),on=column_edge1
)
edge2 = df_edge2[~(df_edge2[node]==df_edge2[node+"_2"])].dropna()[[node,node+"_2",column_edge1]]
edge2.drop(edge2.loc[edge2[node+"_2"]<edge2[node]].index.tolist(),inplace=True)
# Create the connections in the graph
links_attributes = {tuple(row[[node,node+"_2"]]): {column_edge1: row[column_edge1]} for i,row in edge2.iterrows()}
# create the connection,without attribute.
G.add_edges_from(links_attributes)
# adds the attribute.
nx.set_edge_attributes(G=G,values=links_attributes)
if column_edge2:
df_edge3 = df[[node,column_edge2]].dropna(subset=[column_edge2]).drop_duplicates()
df_edge3 = df_edge3.merge(
df_edge3[[node,column_edge2]].rename(columns={node:node+"_2"}),on=column_edge2
)
edge3 = df_edge3[~(df_edge3[node]==df_edge3[node+"_2"])].dropna()[[node,column_edge2]]
edge3.drop(edge3.loc[edge3[node+"_2"]<edge3[node]].index.tolist(),inplace=True)
# Create the connections in the graph
links_attributes2 = {tuple(row[[node,node+"_2"]]): {column_edge2: row[column_edge2]} for i,row in edge3.iterrows()}
# create the connection,without attribute.
G.add_edges_from(links_attributes2)
# adds the attribute.
nx.set_edge_attributes(G=G,values=links_attributes2)
return G
上述函数采用数据框、节点和一个或多个列边,并使用 Networkx
python 包生成图形。
当我一行一行地运行代码时,我没有任何问题。但是,当我尝试在上述函数中运行并返回图形并使用 Pyvis
或 nx_altair
生成交互式绘图时,它会抛出 TypeError: Object of type int64 is not JSON serializable
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-35-b34699ea995d> in <module>()
17 net.from_nx(GE3)
18 # show
---> 19 net.show("pyvis_example.html")
10 frames
/usr/local/lib/python3.7/dist-packages/pyvis/network.py in show(self,name)
474 check_html(name)
475 if self.template is not None:
--> 476 return self.write_html(name,notebook=True)
477 else:
478 self.write_html(name)
/usr/local/lib/python3.7/dist-packages/pyvis/network.py in write_html(self,name,notebook)
457 bgcolor=self.bgcolor,458 conf=self.conf,--> 459 tooltip_link=use_link_template)
460
461 with open(name,"w+") as out:
/usr/local/lib/python3.7/dist-packages/jinja2/environment.py in render(self,*args,**kwargs)
1088 return concat(self.root_render_func(self.new_context(vars)))
1089 except Exception:
-> 1090 self.environment.handle_exception()
1091
1092 def render_async(self,**kwargs):
/usr/local/lib/python3.7/dist-packages/jinja2/environment.py in handle_exception(self,source)
830 from .debug import rewrite_traceback_stack
831
--> 832 reraise(*rewrite_traceback_stack(source=source))
833
834 def join_path(self,template,parent):
/usr/local/lib/python3.7/dist-packages/jinja2/_compat.py in reraise(tp,value,tb)
26 def reraise(tp,tb=None):
27 if value.__traceback__ is not tb:
---> 28 raise value.with_traceback(tb)
29 raise value
30
<template> in top-level template code()
/usr/local/lib/python3.7/dist-packages/jinja2/filters.py in do_tojson(eval_ctx,indent)
1258 options = dict(options)
1259 options["indent"] = indent
-> 1260 return htmlsafe_json_dumps(value,dumper=dumper,**options)
1261
1262
/usr/local/lib/python3.7/dist-packages/jinja2/utils.py in htmlsafe_json_dumps(obj,dumper,**kwargs)
617 dumper = json.dumps
618 rv = (
--> 619 dumper(obj,**kwargs)
620 .replace(u"<",u"\\u003c")
621 .replace(u">",u"\\u003e")
/usr/lib/python3.7/json/__init__.py in dumps(obj,skipkeys,ensure_ascii,check_circular,allow_nan,cls,indent,separators,default,sort_keys,**kw)
236 check_circular=check_circular,allow_nan=allow_nan,indent=indent,237 separators=separators,default=default,sort_keys=sort_keys,--> 238 **kw).encode(obj)
239
240
/usr/lib/python3.7/json/encoder.py in encode(self,o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o,_one_shot=True)
200 if not isinstance(chunks,(list,tuple)):
201 chunks = list(chunks)
/usr/lib/python3.7/json/encoder.py in iterencode(self,o,_one_shot)
255 self.key_separator,self.item_separator,self.sort_keys,256 self.skipkeys,_one_shot)
--> 257 return _iterencode(o,0)
258
259 def _make_iterencode(markers,_default,_encoder,_indent,_floatstr,/usr/lib/python3.7/json/encoder.py in default(self,o)
177
178 """
--> 179 raise TypeError(f'Object of type {o.__class__.__name__} '
180 f'is not JSON serializable')
181
TypeError: Object of type int64 is not JSON serializable
我无法弄清楚类型转换发生的位置或我收到错误的位置。
如何解决上述 TypeError
问题?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)