首页 > 解决方案 > Python pandas:使用条件进行索引

问题描述

我需要画一条连接数据帧中两个点的线(在下表中,一条连接 S01 到 S02、S01 到 S04、S03 到 S04 等的线)点不能明确定义,而是通过迭代数据帧。每行中的坐标是“到”点的坐标。

桌子

这是我迄今为止尝试过的:

df = pd.read_csv(1234.csv')

for index, row in df.iterrows():
    x_coords = [df.loc["from", "x"], df.loc["to", "x"]]
    y_coords = [df.loc["from", "y"], df.loc["to", "y"]]
    plt.plot(x_coords, y_coords,'grey', linewidth=0.5)

标签: pythonpandasdataframeindexing

解决方案


干扰器解决了这个问题:

for i in df.index:
   df.set_index('to')
   a = df.at[i,'From'] #row of from value
   b = a - 1 #correct index value of row
   ax = df.at[b, 'x'] #x value of from
   ay = df.at[b, 'y'] #y of from
   plt.plot([ax,df.at[i,'x']],[ay,df.at[i,'y']],'grey', linewidth=0.5)

推荐阅读