用python pandas连接文件

时间:2014-12-27 08:14:02

标签: python pandas concat

我使用" Python进行数据分析学习数据科学"。

我想将文本文件连接成一个大型数据框。 这是作者提供的代码,但它不能在我的Mac OS X Yosemite上运行.. 有人可以帮忙吗?

import pandas as pd

names1880 = pd.read_csv('ch02/names/yob1880.txt', names=['name', 'sex', 'births'])
names1880.groupby('sex').births.sum()
years = range(1880,2010)

pieces = []
columns = ['name','sex','births']

for year in years:
    path = 'ch02/names/yob%d.txt' % year
    frame = pd.read_csv(path,names=columns)
names = pd.concat(pieces,ignore_index=True)

输出:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-48-76cd5bf6ba3e> in <module>()
     14     frame = pd.read_csv(path,names=columns)
     15 
---> 16 names = pd.concat(pieces,ignore_index=True)
     17 
     18 

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/merge.pyc in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity)
    927                        ignore_index=ignore_index, join=join,
    928                        keys=keys, levels=levels, names=names,
--> 929                        verify_integrity=verify_integrity)
    930     return op.get_result()
    931 

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/merge.pyc in __init__(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity)
    972 
    973         if len(objs) == 0:
--> 974             raise Exception('All objects passed were None')
    975 
    976         # consolidate data

Exception: All objects passed were None

0 个答案:

没有答案