获取DataFrame pandas中的元素位置

68de4m5k  于 2023-04-04  发布在  其他
关注(0)|答案(1)|浏览(118)

我有以下问题:在输入文件“pannelloBschema1_0.t19.glo”上执行一些操作后(“t19.glo”扩展名只是一个.txt文件)我得到变量“min_diff”,其值为0.7222780000000029。我想知道如何得到位置(行),其中"min_diff"是。然后,我需要取放置在同一行中的值,但在其标题为“disp”的列中。我堆叠在这里。我尝试使用index.get_indexer([min_diff]),但它返回:array([-1],dtype= int 64),所以这不是方法。有人能帮我吗?
您可以在这里下载“pannelloBschema1_0.t19.glo”:https://github.com/giadabart/sharedfiles
下面是我的代码:

import numpy as np
import pandas as pd

specificload_reading = np.loadtxt('pannelloBschema1_0.t19.glo', skiprows = 2)

lastline = (specificload_reading[-1][1] + specificload_reading[-1][2])/1000
lastline_reduced = 0.7 * lastline
laoddevelopment = [(el[2]+el[1])/1000 for el in specificload_reading]
dispdevelopment = [-el*1000 for el in specificload_reading[:,0]]

data = {
    "disp": dispdevelopment,
    "load": laoddevelopment,
    "70%load": lastline_reduced,
    "load_diff": laoddevelopment-lastline_reduced
}

df = pd.DataFrame(data)

df['abs_loaddiff']= (df['load'] - df['70%load']).abs()

somerows = df.iloc[0:3]

print(somerows)

min_diff = df['abs_loaddiff'].min()
print(min_diff) #>>> 0.7222780000000029

# index.get_indexer([item], method=…)
index.get_indexer([min_diff])
hmmo2u0o

hmmo2u0o1#

Ciao Giada,欢迎来到SO。如果你能看一下[问],然后尝试制作一个[mcve],那就太好了。如果你能分享一个小样本的数据,而不是一个链接,那就太好了。
无论如何,我建议你采取以下方法。

加载数据

首先直接用pandas读取数据

import pandas as pd

fn = 'https://raw.githubusercontent.com/giadabart/sharedfiles/main/pannelloBschema1_0.t19.glo'

df = pd.read_csv(
    fn,
    skiprows=2,
    delim_whitespace=True,
    names=[0,1,2])

看起来像是

0          1         2
0  0.000417 -12132.530  12132.53
1  0.000395 -11108.940  12914.65
2  0.000374 -10082.920  13694.34
3  0.000353  -9054.317  14471.44
4  0.000332  -8024.002  15246.83

pandas操作而不是list/numpy

df["load"] = (df[1] + df[2]) / 1e3
df["disp"] = -df[0] * 1e3
# Here I'm not quite sure what are you going to archive
# but you can broadcast the result
df["load70%"] = (df.iloc[-1][1] + df.iloc[-1][2]) / 1e3 *.7
# and finally
df["abs_load_diff"] = (df["load"] - df["load70%"]).abs()

现在可以只考虑刚刚计算的最后4列

df = df[['load', 'disp', 'load70%', 'abs_load_diff']]

看起来像是

load      disp      load70%        abs_load_diff
0  0.000000 -0.416960  123.871552     123.871552
1  1.805710 -0.395356  123.871552     122.065842
2  3.611420 -0.373848  123.871552     120.260132
3  5.417123 -0.352667  123.871552     118.454429
4  7.222828 -0.331626  123.871552     116.648724

获取最小值索引

在pandas中,可以通过

df['abs_load_diff'].idxmin()

返回69。您最终可以使用

df.iloc[df['abs_load_diff'].idxmin()]

其返回

load             124.593830
disp               1.452970
load70%          123.871552
abs_load_diff      0.722278
Name: 69, dtype: float64

相关问题