df.loc[df['销量']==0,:].head()
时间: 2023-10-16 12:08:44 浏览: 173
这是 Pandas 库中的一种数据筛选方法,可以选择销量为0的行并输出前五行数据。
其中,df 是一个 Pandas 的 DataFrame 对象,loc 是 DataFrame 的一种数据筛选方法。冒号前的 df['销量']==0 表示选择 销量 列中值为 0 的行,冒号后的逗号表示要选择所有列,head() 方法表示只输出前五行数据。
相关问题
import pandas as pd import networkx as nx import matplotlib.pyplot as plt df = pd.read_excel(r"C:\Users\li'yi'jie\Desktop\运筹学网络规划数据.xlsx") edges = [] for i in range(len(df)): edge = { "id": df.loc[i, "边的编号"], "tail": df.loc[i, "边的尾节点"], "head": df.loc[i, "边的头节点"], "length": df.loc[i, "长度"], "capacity": df.loc[i, "容量"] } edges.append(edge) G = nx.DiGraph() for edge in edges: G.add_edge(edge["tail"], edge["head"], weight=edge["length"]) all_pairs = dict(nx.all_pairs_dijkstra_path_length(G)) pos = nx.spring_layout(G) nx.draw(G, pos, with_labels=True) labels = nx.get_edge_attributes(G, "长度") nx.draw_networkx_edge_labels(G, pos, edge_labels=labels) plt.show()如何修改使生成的网络图的边旁标注权重?
可以在代码最后添加以下语句:
```
labels = nx.get_edge_attributes(G, "weight")
nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)
plt.show()
```
其中 `labels` 变量用于获取图 G 中所有边的权重信息,`nx.draw_networkx_edge_labels()` 函数用于在图形中绘制边旁的权重标签。最后使用 `plt.show()` 函数显示图形即可。
import pandas as pd import networkx as nx import matplotlib.pyplot as plt df = pd.read_excel(r"C:\Users\li'yi'jie\Desktop\运筹学网络规划数据.xlsx") edges = [] for i in range(len(df)): edge = { 'id': df.loc[i, '边的编号'], 'tail': df.loc[i, '边的尾节点'], 'head': df.loc[i, '边的头节点'], 'length': df.loc[i, '长度'], 'capacity': df.loc[i, '容量'] } edges.append(edge) G = nx.DiGraph() for edge in edges: G.add_edge(edge['边的尾节点'], edge['边的头节点'], weight=edge['长度']) all_pairs = dict(nx.all_pairs_dijkstra_path_length(G)) pos = nx.spring_layout(G) nx.draw(G, pos, with_labels=True) labels = nx.get_edge_attributes(G, '长度') nx.draw_networkx_edge_labels(G, pos, edge_labels=labels) plt.show(),报错:KeyError: '边的尾节点',请问如何解决
根据你提供的代码和报错信息,问题出在以下代码段:
```
for i in range(len(df)):
edge = {
'id': df.loc[i, '边的编号'],
'tail': df.loc[i, '边的尾节点'],
'head': df.loc[i, '边的头节点'],
'length': df.loc[i, '长度'],
'capacity': df.loc[i, '容量']
}
edges.append(edge)
```
报错提示是`KeyError: '边的尾节点'`,说明在df中不存在名为'边的尾节点'的列。进一步分析,可能是由于列名中的单引号和双引号混用导致的。你可以将代码中的单引号替换成双引号,或者直接去掉单引号,例如:
```
for i in range(len(df)):
edge = {
"id": df.loc[i, "边的编号"],
"tail": df.loc[i, "边的尾节点"],
"head": df.loc[i, "边的头节点"],
"length": df.loc[i, "长度"],
"capacity": df.loc[i, "容量"]
}
edges.append(edge)
```
或者:
```
for i in range(len(df)):
edge = {
"id": df.loc[i, "边的编号"],
"tail": df.loc[i, "边的尾节点"],
"head": df.loc[i, "边的头节点"],
"length": df.loc[i, "长度"],
"capacity": df.loc[i, "容量"]
}
edges.append(edge)
```
修改后再运行代码看看是否解决了问题。
阅读全文
相关推荐

















