【发布时间】:2021-01-29 12:44:21
【问题描述】:
我在 txt file 中有数据,我将其转换为数据帧 (data3),我将索引重命名为从 -6 运行到 +5,现在,在 for 循环中,我想访问特定值使用 iloc 命令的数据帧,但我没有得到正确的值。
如果我使用 data3.iloc[-6,1],我期望返回值 = -6,但结果却是 -20
data3.iloc[-5,1] 我期待 =-20,但结果却是 -6
data3.iloc[-4,1] 我期待 = -28 但我得到了 -7
有人可以帮帮我吗?将索引从 -6 保留到 +5 对我来说很重要 这是我的代码。谢谢
import numpy as np
import pandas as pd
data= pd.read_csv('perfilprueba.txt',delimiter=' ')
## This is because when I read the txt doesnt read dist and amp as diferent
columns
data_drop = data.drop(data.columns[[1, 2, 3, 4, 6,7]], axis=1)
data2=data_drop.rename(columns={"Unnamed: 5": "amp"})
## These are two index I will use later
m=int(round(len(data2.index)))
n=int(round(m/2))
## This is because I wanted that my data had index values from -6 to 5+ AND
## also a column with values from -6 to +5
r = pd.Series(np.linspace(-n, n-1,m))
data2['r'] = r
erre = pd.Series(np.linspace(-n, n-1,m))
data2['erre']=erre
data3=data2.set_index('r')
## Now I want to run a for loop
## that returns me the values of the "amp" column as r moves from -6 to +5
ap=[]
for r in range(-n,n):
a = data3.loc[[r],['amp']]
ap += [a]
【问题讨论】:
标签: python pandas for-loop indexing