我正在尝试运行此代码但每次都会给我同样的错误。
def concatenate(indir="E:\\HOME\\NBAStats\\PlayByPlay",outfile="E:\\HOME\\NBAStats\\PlayByPlay\\Concatenated.csv",sep='\t'):
os.chdir(indir)
filelist=glob.glob("*.csv")
dflist=[]
colnames=['game_id', 'data_set', 'date', 'a1', 'a2', 'a3', 'a4', 'a5', 'h1', 'h2', 'h3', 'h4', 'h5', 'period', 'away_score', 'home_score', 'remaining_time', 'elapsed', 'play_length', 'play_id', 'team', 'event_type', 'assist', 'away', 'home', 'block', 'entered', 'left', 'num', 'opponent', 'outof', 'player', 'points', 'possession', 'reason', 'result', 'steal', 'type', 'shot_distance', 'original_x', 'original_y', 'converted_x', 'converted_y', 'description']
for filename in filelist:
print(filename)
df=pandas.read_table(filename, header=None)
dflist.append(df)
concatdf=pandas.concat(dflist,axis=0)
print(type(concatdf))
concatdf.columns=colnames
concatdf.head()
concatdf.to_csv(outfile,index=False)
错误是:
ValueError:长度不匹配:预期的轴有1个元素,即新值 有44个元素
答案 0 :(得分:0)
这是使用read_csv而不是read_table
发生的Vale错误> File "<ipython-input-8-24ad9c1ee739>", line 1, in <module>
> runfile('C:/Users/Fede/ToConcatenateNBA.py', wdir='C:/Users/Fede')
>
> File
> "C:\Users\Fede\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
> line 880, in runfile
> execfile(filename, namespace)
>
> File
> "C:\Users\Fede\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
> line 102, in execfile
> exec(compile(f.read(), filename, 'exec'), namespace)
>
> File "C:/Users/Fede/ToConcatenateNBA.py", line 29, in <module>
> concatenate()
>
> File "C:/Users/Fede/ToConcatenateNBA.py", line 25, in concatenate
> concatdf.columns=colnames
>
> File
> "C:\Users\Fede\Anaconda3\lib\site-packages\pandas\core\generic.py",
> line 2983, in __setattr__
> return object.__setattr__(self, name, value)
>
> File "pandas\_libs\src\properties.pyx", line 65, in
> pandas._libs.lib.AxisProperty.__set__ (pandas\_libs\lib.c:45103)
>
> File
> "C:\Users\Fede\Anaconda3\lib\site-packages\pandas\core\generic.py",
> line 471, in _set_axis
> self._data.set_axis(axis, labels)
>
> File
> "C:\Users\Fede\Anaconda3\lib\site-packages\pandas\core\internals.py",
> line 2836, in set_axis
> (old_len, new_len))
>
> ValueError: Length mismatch: Expected axis has 46 elements, new values
> have 44 elements