我已将DataFrame
从ipython笔记本传递到标准.py
文件中的函数。
在函数中,我正在使用df['column_name'].values
尝试从该列中提取所有值。
在调试时(在pycharm中),我在pycharm提供的“评估”工具中运行此行,并且工作正常。但是,当我正常运行同一行(在工具窗口外部)时,出现错误:
“ TypeError:列表索引必须是整数或切片,而不是str”
在工作区变量部分中查看我的数据框时,它被正确解释为一个数据框。
我要传递的数据框对象有3列(“ x”,“ y”和“ likelyhood”),每列包含整数/浮点数。将其中的一列中的值解压缩的相关行是该函数的第一行。
谁能解释这是怎么发生的?导致一行代码可以在一个代码中工作而在另一个代码中返回错误的事实有何不同?
以及可能导致此特定TypeError
的原因以及如何解决此错误?
df.tail(10)打印的数据框:
coords x y likelihood
105570 297.497525 332.355521 1.0
105571 297.463208 332.353797 1.0
105572 297.439774 332.383908 1.0
105573 297.457581 332.458205 1.0
105574 297.487260 332.402202 1.0
105575 297.519772 332.451551 1.0
105576 297.495998 332.431064 1.0
105577 297.516722 332.113481 1.0
105578 297.542539 332.080923 1.0
105579 297.528317 332.046282 1.0
全功能:
import math
import numpy as np
import pandas as pd
def filter_jumps(bp, thresh=10):
"""
the function watches for jumps that cannot happen, and interpolates the location of the point acoordingly
"""
x=bp['x'].values
y=bp['y'].values
for i in range(1,len(x)):
if np.abs(x[i]+y[i]-x[i-1]-y[i-1]) > thresh:
start = i-1; end = None; idx=i+1
while not end:
if np.abs(x[idx]+y[idx]-x[idx-1]-y[idx-1]) > thresh:
end = idx
else:
idx += 1
rang = end-start
x[start:end] = np.linspace(x[start], x[end], rang)
y[start:end] = np.linspace(y[start], y[end], rang)
bp['x'] = x
bp['y'] = y
return bp