pandas python pandas数据帧索引,错误TypeError:输入必须是可迭代的,pandas版本可能错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38487261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python pandas dataframe index, error TypeError: Input must be iterable, pandas version perhaps wrong
提问by Matthijs Noordzij
I'm working with the eda-explorer python library from MIT, which allows one to import physiological data files from particular wearable biosensors. This libraray uses pandas DataFrames to store the physiological timeseries. I've been using this libarary in different computing set-ups. When I try to use it in my ubuntu 15.10 environment I get an error message I don't understand. It is related to the following function which is instrumental in getting the data into a DataFrame and doing some intitial transformations:
我正在使用麻省理工学院的 eda-explorer python 库,它允许从特定的可穿戴生物传感器导入生理数据文件。该库使用 Pandas DataFrames 来存储生理时间序列。我一直在不同的计算设置中使用这个库。当我尝试在我的 ubuntu 15.10 环境中使用它时,我收到一条我不明白的错误消息。它与以下函数有关,该函数有助于将数据放入 DataFrame 并进行一些初始转换:
def loadData_E4(filepath):
# Load data
data = pd.DataFrame.from_csv(os.path.join(filepath,'EDA.csv'))
data.reset_index(inplace=True)
# Get the startTime and sample rate
startTime = pd.to_datetime(float(data.columns.values[0]),unit="s")
sampleRate = float(data.iloc[0][0])
data = data[data.index!=0]
data.index = data.index-1
This results in the following error messages:
这会导致以下错误消息:
In [1]:
run batch_edaexplorer_template.py
Classifying data for ...[my file location]...
---------------------------------------------------------------------
TypeError Traceback (most recent call last)
/...mypath/eda-explorer-master/batch_edaexplorer_template.py in <module>()
69 elif dataType=='e4':
70 print "Classifying data for " + filepath
---> 71 labels,data = classify(filepath,classifierList,pickleDirectory,lf.loadData_E4)
72 elif dataType=="misc":
73 print "Classifying data for " + filepath
/...mypath/eda-explorer-master/EDA_Artifact_Detection_Script.pyc in classify(filepath, classifierList, pickleDirectory, loadDataFunction)
225
226 # Load data
--> 227 data = loadDataFunction(filepath)
228
229 # Get pickle List and featureNames list
/...mypath/eda-explorer-master/load_files.pyc in loadData_E4(filepath)
58 sampleRate = float(data.iloc[0][0])
59 data = data[data.index!=0]
---> 60 data.index = data.index-1
61
62 # Reset the data frame assuming 4Hz samplingRate
/usr/lib/python2.7/dist-packages/pandas/core/index.pyc in __sub__(self, other)
1161 warnings.warn("using '-' to provide set differences with Indexes is deprecated, "
1162 "use .difference()",FutureWarning)
-> 1163 return self.difference(other)
1164
1165 def __and__(self, other):
/usr/lib/python2.7/dist-packages/pandas/core/index.pyc in difference(self, other)
1314
1315 if not hasattr(other, '__iter__'):
-> 1316 raise TypeError('Input must be iterable!')
1317
1318 if self.equals(other):
TypeError: Input must be iterable!
I don't get this error message on my windows PC. I'm using pandas version 0.15.0 in the ubuntu environment. Is this perhaps the problem that the particular syntax related to the index is only allowed in higher versions of pandas? How should I correct the syntax so that it works with older version of pandas? Or am I missing the point?
我的 Windows PC 上没有收到此错误消息。我在 ubuntu 环境中使用 Pandas 0.15.0 版。这可能是与索引相关的特定语法仅在更高版本的Pandas中才允许的问题吗?我应该如何更正语法以使其适用于旧版本的Pandas?还是我错过了重点?
回答by Alberto Garcia-Raboso
Try data.index = pd.Index(data.index.values-1)
instead of data.index = data.index-1
.
尝试data.index = pd.Index(data.index.values-1)
代替data.index = data.index-1
.