InvalidIndexError:重索引仅对唯一赋值的Index对象有效 [英] InvalidIndexError: Reindexing only valid with uniquely valued Index objects
问题描述
使用Python,我很难将208个CSV文件合并到一个数据帧中。 (我的文件名称为Customer_1.csv,Customer_2.csv ,,,和Customer_208.csv)
以下是我的代码,
%matplotlib inline
import pandas as pd
df_merged = pd.concat([pd.read_csv('data_TVV1 / Customer_ {0} .csv'范围内的i.format(i),names = ['Time','Energy_ {0}'format(i)],parse_dates = ['Time'],index_col = ['Time'],skiprows = 1) (1,209)],axis = 1)
InvalidIndexError回溯(最近一次调用)
< ipython-input-4-a4d19b3c2a3e> in< module>()
----> 1 df_merged = pd.concat([pd.read_csv('data_TVV1 / Customer_ {0} .csv'.format(i),names = ['Time','Energy_ {0}'。format(i)],parse_dates = (1,209)],轴= 1)
/ Users / Suzuki / Envs / DataVizProj /
752 keys = keys,levels()函数,如下所示:lib / python2.7 / site-packages / pandas / tools / merge.pyc concat(objs,axis,join,join_axes,ignore_index,keys,levels,names,verify_integrity, = levels,names = names,
753 verify_integrity = verify_integrity,
- > 754 copy = copy)
755 return op.get_result()
756
/Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/pandas/
884 self.copy = copy
885
- 在$ __init中的工具/ merge.pyc __(self,objs,axis,join,join_axes,keys,levels,names,ignore_index,verify_integrity, - > 886 self.new_axes = self._get_new_axes()
887
888 def get_result(self):
/Users/Suzuki/Envs/DataVizProj/lib/python2.7/site -packages / pandas / tools / merge.pyc在_get_new_axes(self)
944如果i == self.axis:
945 continue
- > 946 new_axes [i] = self._get_comb_axis(i)
947 else:
948 if len(self.join_axes)!= ndim - 1:
/ Users / Suzuki /
970 raise TypeError(Can not concatenate list of%s%types)$ b $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b 971
- > 972 return _get_combined_index(all_indexes,intersect = self.intersect)
973
974 def _get_concat_axis(self):
/Users/Suzuki/Envs/DataVizProj/lib/python2.7 /site-packages/pandas/core/index.pyc在_get_combined_index(索引,相交)
5730 index = index.intersection(other)
5731 return index
- > 5732 union = _union_indexes(indexes)
5733 return _ensure_index(union)
5734
/Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/pandas /core/index.pyc在_union_indexes(索引)
5759
5760如果hasattr(result,'union_many'):
- > 5761返回result.union_many(索引[1:])
5762其他:
5763其他索引[1:]:
/ Users / Suzuki / Envs / DataVizProj / lib / python2.7 / site-packages / pandas / tseries / index.pyc在union_many(self,others)
847 else:
848 tz = this.tz
- > 849 this = Index.union(this,other)
850 if isinstance(this,DatetimeIndex):
851 this.tz = tz
/ Users / Suzuki / Envs / DataVizProj /lib/python2.7/site-packages/pandas/core/index.pyc in union(self,other)
1400 result.extend([x for other.values if x not in value_set])
1401 else:
- > 1402 indexer = self.get_indexer(other)
1403 indexer,=(indexer == -1).nonzero()
1404
/ Users / Suzuki / Envs / DataVizProj /
1685
1686如果不是self.is_unique:
- > ; 1687 raise InvalidIndexError('Reindexing only only valid with unique'
1688'value'Index'objects')
1689
InvalidIndexError:重新索引只对唯一有价值的索引对象有效
你有什么想法来解决这个问题吗?
解决方案您的代码适用于我用于测试的五个文件的一个小样本(每个文件包含两列和三行)。只有调试,尝试写在一个for循环。首先,在循环之前,将所有文件读入列表。然后再次循环并使用
try / except
块来追加每个错误。最后,打印问题文件并进行调查。#首先,将所有文件读入列表。
files_in = [pd.read_csv('data_TVV1 / Customer_ {0} .csv'.format(i),
names = ['Time','Energy_ {0} ,
parse_dates = ['Time'],
index_col = ['Time'],
skiprows = 1)
for i in range(1,209)]
df = pd.DataFrame()
errors = []
#尝试将每个文件附加到数据框。
for ii range(1,209):
try:
df = pd.concat([df,files_in [i-1]],axis = 1)
errors.append(i)
#打印包含错误的文件。
错误错误:
print(files_in [error])
Using Python, I am struggling to merge 208 CSV files into one dataframe. (My files names are Customer_1.csv, Customer_2.csv,,, and Customer_208.csv)
Following are my codes,
%matplotlib inline import pandas as pd df_merged = pd.concat([pd.read_csv('data_TVV1/Customer_{0}.csv'.format(i), names = ['Time', 'Energy_{0}'.format(i)], parse_dates=['Time'], index_col=['Time'], skiprows=1) for i in range(1, 209)], axis=1)
I got an error saying,
InvalidIndexError Traceback (most recent call last) <ipython-input-4-a4d19b3c2a3e> in <module>() ----> 1 df_merged = pd.concat([pd.read_csv('data_TVV1/Customer_{0}.csv'.format(i), names = ['Time', 'Energy_{0}'.format(i)], parse_dates=['Time'], index_col=['Time'], skiprows=1) for i in range(1, 209)], axis=1) /Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/pandas/tools/merge.pyc in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy) 752 keys=keys, levels=levels, names=names, 753 verify_integrity=verify_integrity, --> 754 copy=copy) 755 return op.get_result() 756 /Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/pandas/tools/merge.pyc in __init__(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity, copy) 884 self.copy = copy 885 --> 886 self.new_axes = self._get_new_axes() 887 888 def get_result(self): /Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/pandas/tools/merge.pyc in _get_new_axes(self) 944 if i == self.axis: 945 continue --> 946 new_axes[i] = self._get_comb_axis(i) 947 else: 948 if len(self.join_axes) != ndim - 1: /Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/pandas/tools/merge.pyc in _get_comb_axis(self, i) 970 raise TypeError("Cannot concatenate list of %s" % types) 971 --> 972 return _get_combined_index(all_indexes, intersect=self.intersect) 973 974 def _get_concat_axis(self): /Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/pandas/core/index.pyc in _get_combined_index(indexes, intersect) 5730 index = index.intersection(other) 5731 return index -> 5732 union = _union_indexes(indexes) 5733 return _ensure_index(union) 5734 /Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/pandas/core/index.pyc in _union_indexes(indexes) 5759 5760 if hasattr(result, 'union_many'): -> 5761 return result.union_many(indexes[1:]) 5762 else: 5763 for other in indexes[1:]: /Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/pandas/tseries/index.pyc in union_many(self, others) 847 else: 848 tz = this.tz --> 849 this = Index.union(this, other) 850 if isinstance(this, DatetimeIndex): 851 this.tz = tz /Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/pandas/core/index.pyc in union(self, other) 1400 result.extend([x for x in other.values if x not in value_set]) 1401 else: -> 1402 indexer = self.get_indexer(other) 1403 indexer, = (indexer == -1).nonzero() 1404 /Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/pandas/core/index.pyc in get_indexer(self, target, method, limit) 1685 1686 if not self.is_unique: -> 1687 raise InvalidIndexError('Reindexing only valid with uniquely' 1688 ' valued Index objects') 1689 InvalidIndexError: Reindexing only valid with uniquely valued Index objects
Do you have any idea to solve this problem???..
解决方案Your code works on a small sample of five files that I used for testing (each file containing two columns and three row). ONLY FOR DEBUGGING, try to write this in a for loop. First, before the loop, read all of the files into the list. Then loop again and append each one using a
try/except
block to catch the errors. Finally, print the problem files and investigate.# First, read all the files into a list. files_in = [pd.read_csv('data_TVV1/Customer_{0}.csv'.format(i), names = ['Time', 'Energy_{0}'.format(i)], parse_dates=['Time'], index_col=['Time'], skiprows=1) for i in range(1, 209)] df = pd.DataFrame() errors = [] # Try to append each file to the dataframe. for i i range(1, 209): try: df = pd.concat([df, files_in[i - 1]], axis=1) except: errors.append(i) # Print files containing errors. for error in errors: print(files_in[error])
这篇关于InvalidIndexError:重索引仅对唯一赋值的Index对象有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!