Pandas:如果我只知道DataFram的一行中的一个元素,如何访问该行中的其他元素

2024-10-02 00:33:57 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,在数据框中:

Gyroscope   448083  -0.05965481 -0.26418558 -0.017044231
Gyroscope   448338  -0.033023197    -0.18002969 -0.011717909
Gyroscope   448340  -0.052197956    -0.17470336 -0.049002163
Gyroscope   448341  -0.03834952 -0.16937704 -0.034088463

我知道第二栏的元素,但我想把最后三个元素去掉。你知道吗

Interval是一个元组,我知道开始和结束的时间戳,但这些值可能不在数据帧中。 如果我的interval[0]变量是448075,而我的interval[1]变量是448345,那么如何访问列1值在interval[0]interval[1]之间的行的3rd4th5th列中的所有元素?你知道吗

我试图在实际的DataFrame中找到最接近interval[0]interval[1]的值。你知道吗

start = np.abs(merge.iloc[:, 1] - i[0]).argmin()
end = np.abs(merge.iloc[:, 1] - i[1]).argmin()

for j in range(start, end):
    gyro.append((merge.iloc[:, j][2], merge.iloc[:, j][3], merge.iloc[:, j][4])) if merge.iloc[:, j][0] == 'Gyroscope' \
        else acc.append((merge.iloc[:, j][2], merge.iloc[:, j][3], merge.iloc[:, j][4]))

然后,使用间隔中的数字范围,我尝试提取每行中的最后三个条目。你知道吗

gyroacc是我在相关行中添加最后三个条目的列表。你知道吗

merge.head()返回

            Gyroscope    448083  -0.05965481  -0.26418558  -0.017044231
0           Gyroscope  448338.0    -0.033023    -0.180030     -0.011718
1  LinearAcceleration  448339.0    -0.048183    -0.427365     -0.019752
2           Gyroscope  448340.0    -0.052198    -0.174703     -0.049002
3  LinearAcceleration  448340.0     0.168791    -0.166547     -0.136918
4           Gyroscope  448341.0    -0.038350    -0.169377     -0.034088

基本上,我想根据第二列值的范围建立索引,并遍历它。你知道吗


Tags: 数据元素npabsmergestartendacc

热门问题