【问题标题】:Sorting a CSV-file into different CSVs by column value按列值将 CSV 文件排序为不同的 CSV
【发布时间】:2018-07-03 13:54:08
【问题描述】:

在编程方面,我仍然是一个初学者,我的代码遇到了一些问题。在这里搜索解决方案,但遗憾的是没有任何帮助。

我正在尝试做的事情: 我有一个 csv 文件(我从多个 txt.files 导入)。我的一个专栏列出了从 2015 年到 1991 年的年份,我想根据相应的年份将文件的所有行排序到不同的 csv 中。 我当前的代码看起来像这样(尽管我对其进行了相当多的更改,尝试使用我在这方面找到的提示)

einzel = pd.read_csv("501-1000.csv", sep='\t',header=0,index_col=False,usecols = ("TI","AB","PY","DI"),dtype = str)

with open("501-1000.csv", "r",encoding="utf_8"):

    for row in einzel:
        if einzel["PY"] == ["2015","2014","2013","2012","2011"]:
            with open("a.csv","a") as out:
                writer.writerow(row)
        elif einzel["PY"] ==  ["2010","2009","2008","2007","2006"]:
            with open("b.csv","a") as out:
                writer.writerow(row)
        elif einzel["PY"] ==  ["2005","2004","2003","2002","2001"]:
            with open("c.csv","a") as out:
                writer.writerow(row)
        elif einzel["PY"] ==  ["2000","1999","1998","1997","1996"]:
            with open("d.csv","a") as out:
                writer.writerow(row)
        elif einzel["PY"] ==  ["1995","1994","1993","1992","1991"]:
            with open("e.csv","a") as out:
                writer.writerow(row)

现在...这不起作用,我收到一个错误

ValueError:数组长度不同:489 vs 5

回溯是

ValueError                                Traceback (most recent call last)
<ipython-input-10-72280961cb7d> in <module>()
 19    # writer = csv.writer(out)
 20     for row in einzel:
---> 21         if einzel["PY"] == ["2015","2014","2013","2012","2011"]:
 22             with open("a.csv","a") as out:
 23                 writer.writerow(row)

~\Anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(self, other, axis)
859 
860             with np.errstate(all='ignore'):
--> 861                 res = na_op(values, other)
862             if is_scalar(res):
863                 raise TypeError('Could not compare %s type with Series' %

~\Anaconda3\lib\site-packages\pandas\core\ops.py in na_op(x, y)
763 
764         if is_object_dtype(x.dtype):
--> 765             result = _comp_method_OBJECT_ARRAY(op, x, y)
766         else:
767 

~\Anaconda3\lib\site-packages\pandas\core\ops.py in _comp_method_OBJECT_ARRAY(op, x, y)
741             y = y.values
742 
--> 743         result = lib.vec_compare(x, y, op)
744     else:
745         result = lib.scalar_compare(x, y, op)

pandas\_libs\lib.pyx in pandas._libs.lib.vec_compare()

ValueError: Arrays were different lengths: 489 vs 5

我在这里搜索了错误,但遗憾的是没有一个解决方案有效,或者我不理解它们。 我开始使用类似的东西,但也没有用..

with open("501-1000.csv", "r",encoding="utf_8") as inp:
#reader = csv.reader(inp)
#writer = csv.writer(out)

如果我提出问题的方式有任何问题,我会很高兴得到任何提示或更正,我会纠正它。第一次发帖等等。

【问题讨论】:

  • 你在用熊猫吗?另外:发布完整的回溯,而不仅仅是错误。单独的错误对于了解导致问题的原因没有用处
  • 是的,我正在使用熊猫。将在其中编辑回溯,抱歉错过了!
  • 下次为熊猫问题添加pandas标签。另外:编辑问题后,请确保代码缩进是正确的(for 循环中的所有if 都没有缩进)
  • 更正了 indetation,不知道为什么没有更正。感谢您指出熊猫标签,忘记那个了。

标签: python arrays python-3.x pandas csv


【解决方案1】:

这是一个熊猫解决方案。

import pandas as pd

filemap_dict = {'a': set(range(2011, 2016)),
                'b': set(range(2006, 2011)),
                'c': set(range(2001, 2006)),
                'd': set(range(1996, 2001)),
                'e': set(range(1991, 1996))}

# check your mappings are mutually exclusive
assert not set.intersection(*list(filemap_dict.values())), "Year ranges are not mutually exclusive!"

# load data; note dtype not set to str since there appear to be numeric columns
cols = ['TI', 'AB', 'PY', 'DI']
df = pd.read_csv('501-1000.csv', sep='\t', header=None, index_col=False, names=cols, usecols=cols)

# cycle through filename_dict, slice and export to csv
for k, v in filemap_dict.items():
    df[df['PY'].isin(v)].to_csv(k+'.csv', index=False)

【讨论】:

  • 我必须导入其他东西吗?目前它告诉我 filemap_dict 没有定义。
  • 是的,我可以看到。我仍然在这一行得到一个错误: 16 # 在 filename_dict 中循环,切片并导出到 csv ---> 17 for k, v in file_map_dict.items():。它告诉我,名称没有定义。
  • 我刚刚纠正了一个错字,从 file_map_dict 到 filemap_dict。
  • 更正此错误可以解决错误(我自己可能已经看到了..)但是读取其中一个输出 csv 将它们返回为空? (我用 test = pd.read_csv("b.csv") 和 test.tail()) 进行了尝试
  • 您必须检查年份列是否以整数而不是字符串形式读取。如果以字符串形式读入,您可能需要执行df['PY'].astype(int).isin(v)。代码应该可以工作,但您必须对数据类型进行一些完整性检查。
猜你喜欢
  • 2019-10-28
  • 2021-09-04
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多