![](/img/trans.png)
[英]How to read a csv in Python using Pandas' read_csv after a certain line having specific sting?
[英]How to use Pandas with Python Networkx using read_csv?
我有一个CSV文件,使用Networkx和Pandas库创建网络时必须使用该文件,数据为(net.csv)-
,1,2,3,4,5,6,7,8,9,10
1,0,0.257905291,0.775104118,0.239086843,0.002313744,0.416936603,0.194817214,0.163350301,0.252043807,0.251272559
2,0.346100279,0,0.438892758,0.598885794,0.002263231,0.406685237,0.523850975,0.257660167,0.206302228,0.161385794
3,0.753358102,0.222349243,0,0.407830809,0.001714776,0.507573592,0.169905687,0.139611318,0.187910832,0.326950557
4,0.185342928,0.571302688,0.51784403,0,0.003231018,0.295197533,0.216184462,0.153032751,0.216331326,0.317961522
5,0,0,0,0,0,0,0,0,0,0
6,0.478164621,0.418192795,0.646810223,0.410746629,0.002414973,0,0.609176897,0.203461461,0.157576977,0.636747837
7,0.24894327,0.522914349,0.33948832,0.316240267,0.002335929,0.639377086,0,0.410011123,0.540266963,0.587764182
8,0.234017887,0.320967208,0.285193773,0.258198079,0.003146737,0.224412057,0.411725737,0,0.487081815,0.469526333
9,0.302955306,0.080506624,0.261610132,0.22856311,0.001746979,0.014994905,0.63386228,0.486096957,0,0.664434415
10,0.232675407,0.121596312,0.457715027,0.310618067,0.001872929,0.57556548,0.473562887,0.32185564,0.482351246,0
仅供参考,它以这种有序的方式表示-
1 2 3 4 5 6 7 8 9 10
1 0 0.257905291 0.775104118 0.239086843 0.002313744 0.416936603 0.194817214 0.163350301 0.252043807 0.251272559
2 0.346100279 0 0.438892758 0.598885794 0.002263231 0.406685237 0.523850975 0.257660167 0.206302228 0.161385794
3 0.753358102 0.222349243 0 0.407830809 0.001714776 0.507573592 0.169905687 0.139611318 0.187910832 0.326950557
4 0.185342928 0.571302688 0.51784403 0 0.003231018 0.295197533 0.216184462 0.153032751 0.216331326 0.317961522
5 0 0 0 0 0 0 0 0 0 0
6 0.478164621 0.418192795 0.646810223 0.410746629 0.002414973 0 0.609176897 0.203461461 0.157576977 0.636747837
7 0.24894327 0.522914349 0.33948832 0.316240267 0.002335929 0.639377086 0 0.410011123 0.540266963 0.587764182
8 0.234017887 0.320967208 0.285193773 0.258198079 0.003146737 0.224412057 0.411725737 0 0.487081815 0.469526333
9 0.302955306 0.080506624 0.261610132 0.22856311 0.001746979 0.014994905 0.63386228 0.486096957 0 0.664434415
10 0.232675407 0.121596312 0.457715027 0.310618067 0.001872929 0.57556548 0.473562887 0.32185564 0.482351246 0
我有一个应该使用这些值创建网络的代码,但它不起作用(networkx3.py)-
import matplotlib.pyplot as plt
import pandas as pd
import networkx as nx
g = nx.Graph()
connection_success_rates = pd.read_csv('net.csv', index_col=[0])
connection_success = connection_success_rates.values.tolist()
temp = 1
for row in connection_success:
for i in row:
if type(row[i]) is str:
g.add_edge(temp, int(i), weight=(float(row[i])))
temp = temp+1
pos = nx.spring_layout(g, scale=100.)
nx.draw_networkx_nodes(g, pos)
nx.draw_networkx_edges(g, pos)
nx.draw_networkx_labels(g, pos)
plt.show()
我不知道出什么问题了,每当我运行它时,都会引发以下错误-
Traceback (most recent call last):
File "networkx3.py", line 13, in <module>
if type(row[i]) is str:
TypeError: list indices must be integers, not float
(我在编写@Adonis的原始代码时有所帮助。这是修改后的版本,当然不起作用)
每当我想进行小的修改时,我也会遇到不同的错误。
谁能帮助我找出问题所在? (我不想修改CSV文件或其值)。 第一行/标题和第一列/索引也将被忽略。
在此先感谢您的帮助:)(使用Ubuntu 14.04 32位VM)
for i in row
的for i in row
提取元素,而不是其索引。 但是,由于还需要元素索引,因此应使用enumerate(row)
。 因此,您可以执行以下操作:
import matplotlib.pyplot as plt
import pandas as pd
import networkx as nx
g = nx.Graph()
connection_success_rates = pd.read_csv('net.csv', index_col=[0])
connection_success = connection_success_rates.values.tolist()
temp = 1
for row in connection_success:
for idx, i in enumerate(row):
if type(i) is str:
g.add_edge(temp, idx, weight=(float(i)))
temp = temp+1
pos = nx.spring_layout(g, scale=100.)
nx.draw_networkx_nodes(g, pos)
nx.draw_networkx_edges(g, pos)
nx.draw_networkx_labels(g, pos)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.