如果在另一列中满足条件,则将列值附加到列表

无法完全找到解决方案,这似乎很基本,但说我有:

df ={'ID' : [1, 1, 1, 1, 1, 1, 1, 2, 2],
     'x':[76.551, 77.529, 78.336,77, 76.02, 79.23, 77.733, 79.249,  76.077],
     'y': [151.933, 152.945, 153.970, 119.369, 120.615, 118.935, 119.115, 152.004, 153.027],
    'position': ['start', 'end', 'start', 'NA', 'NA','NA','end', 'start', 'end']}
df = pd.DataFrame(df)

df
   ID       x        y position
0   1  76.551  151.933    start
1   1  77.529  152.945      end
2   1  78.336  153.970    start
3   1  77.000  119.369       NA
4   1  76.020  120.615       NA
5   1  79.230  118.935       NA
6   1  77.733  119.115      end
7   2  79.249  152.004    start
8   2  76.077  153.027      end

我想用“开始”和“停止”点绘制唯一 ID 的路径。我想我可以这样做:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

cmap = cm.get_cmap('rainbow')
colors = cmap(np.linspace(0, 1, df['ID'].nunique()))
color_map = dict(zip(df['ID'].unique(), colors))

# fig, ax = plt.subplots()
plt.figure(figsize=(10,10))
for ID, subdf in df.groupby('ID'):
    plt.plot(subdf['x'], subdf['y'], marker=',', label=None, c=color_map[ID], linewidth=0.1)
# ax.legend()

plt.plot(x_start, y_start, 'og') # plot the start coords in green?
plt.plot(x_stop, y_stop, 'or') # plot the end coords in red?
plt.show()

但我无法弄清楚如何将开始和停止坐标附加到一个空数组......我认为这会起作用,但它没有:

x_start = []
x_end = []
y_start = []
y_end = []

for i in range(len(df)):
    if df.loc[df['position'] == 'start']:
        x_start.append(df['x'][i])
        y_end.append(df['y'][i])
    if df.loc[df['position'] == 'end']:
        x_end.append(df['x'][i])
        y_end.append(df['y'][i])
畜栏

您是否在寻找:

x_start, y_start = df.loc[df['position'] == 'start', ['x', 'y']].values.T.tolist()
x_end, y_end = df.loc[df['position'] == 'end', ['x', 'y']].values.T.tolist()

输出:

>>> x_start
[76.551, 78.336, 79.249]

>>> y_start
[151.933, 153.97, 152.004]

>>> x_end
[77.529, 77.733, 76.077]

>>> y_end
[152.945, 119.115, 153.027]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python Pandas:如果在另一列中满足条件,则将方法应用于列

如果在另一列中满足条件,则使用 sed 替换 csv 列中的值

如果在SQL的另一列中满足条件,如何对一列中的单元格求和

如果在另一列上满足条件,则 Pandas 函数可对一列执行计算

SQL:如果满足条件,则将值从列复制到同一列中的另一行

如果在另一列中为False,如何使列值为负

如果满足条件,则用另一列中的值替换一列中的行

检查条件是否为真,如果是,则将值添加到 sql 中的另一列

R:如果在另一列中数据点也为 NA,则将一列中的值更改为 NA

Pandas - 如果在一行中满足条件,则将值添加到前一行而不进行迭代

如果满足条件,则将列总和分配给另一列中的一个日期

如果在另一列(c)中找到另一列(b)中的值,则在第一列(a)中输入文本

如果在Google表格中满足条件,则将边框格式添加到行

熊猫:如果日期时间索引中的日期不同,则将一列的值添加到另一列

熊猫:如果存在索引,则将一列的值添加到另一列

如果在R中满足条件,则用多列中的NA替换值

如果在一列中重复了值,我如何将另一列中的某些值提取到新列中?

VBA - 如果满足条件,则将公式放入另一列

如果在Pandas Python中单元格值为-1,如何放置另一列的值

如果一列中的前一个值和另一列中的下一个值满足条件,则使用 r 将 1 添加到另一列中

Python:如果满足条件,则用另一列表中的元素替换

如果在另一个列表中找到值,则不会将Python列表附加到后面

如果在MySql中退出,则替换为另一列

如果满足条件,则将列总和分配给另一列中一个日期的月份

如果在另一个数据框列pandas中找到一列中的值,则返回值

如果在一列中具有相同的值,则连接行

如果在特定编号的列(eventx)中value为true,则在名称为(dayx)的x值相同的另一列中返回值

如果满足条件,则将列表系列中的-n元素追加到另一个列表系列中

如果不满足另一列中的条件,尝试从pandas列中返回值