我以前在研究覆盆子皮,但一旦我搬到了一台Ubuntu电脑,下面的代码就不再工作了。我想我需要以某种方式安装csvformat,但不知道如何安装。Python代码:import osimport subprocessimport pandasimport pandas as pdsubprocess.call("csvformat -t plates.txt >> plates.csv", shell=True)f=pd.read_csv("plates.csv")keep_col = [11,13]new_f = f[keep_col]new_f.to_csv("new_plates.csv", index=False)subprocess.call("sudo rm plates.csv", shell=True)df = pd.read_csv("new_plates.csv", names=["Plates", "Name"])df["Plates"] = df["Plates"].str.split().apply("".join)print(df)错误消息:/bin/sh: 1: csvformat: not foundTraceback (most recent call last): File "script.py", line 14, in <module> new_f = f[keep_col] File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 2934, in __getitem__ raise_missing=True) File "/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py", line 1354, in _convert_to_indexer return self._get_listlike_indexer(obj, axis, **kwargs)[1] File "/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py", line 1161, in _get_listlike_indexer raise_missing=raise_missing) File "/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py", line 1246, in _validate_read_indexer key=key, axis=self.obj._get_axis_name(axis)))KeyError: "None of [Int64Index([11, 13], dtype='int64')] are in the [columns] 最佳答案 你有两个不同的,无关的问题。第一个问题:/bin/sh: 1: csvformat: not found您的脚本似乎在寻找csvformat命令行工具,它是csvkit的一部分。你可以:1.2条。安装csvkit安装csvkit很容易:sudo pip install csvkit注:如果您熟悉install csvkit with pip,最好将csvkit安装到它自己的环境。如果你这样做了,你就应该离开前一个命令中的sudo。正如csvkit文档(和注释)中所述,通常认为执行sudo pip install是不好的。见virtualenv。建议使用虚拟环境。安装csvkit后,请检查您现在是否具有csvformat:csvformat -h现在,在运行脚本时,请确保用于安装csvkit的pip与用于运行脚本的相同Python(python,python3,…)。例如,如果使用python运行脚本:$ python -m pip list | grep csvkitcsvkit 1.0.4如果未显示csvkit,则将其安装到其他位置。第二个问题:KeyError: "None of [Int64Index([11, 13], dtype='int64')] are in the [columns]您得到这个是因为您错误地访问(切片)了f的内容。What are the risks of running 'sudo pip'?的返回是一个pandas数据帧。你不能只用read_csv来分割它。您需要使用(基于标签的切片)或f[11,13](基于整数索引的切片)。我不知道你到底想用loc做什么,但你应该这样做:f=pd.read_csv("test.csv")# 1 .. 11 12 13 ..# 0 x .. x x x ..# 1 x .. x x x ..# 2 x .. x x x ..# ..f_slice_1 = f.loc[:, '11':'13']# 11 12 13# 0 x x x# 1 x x x# 2 x x x# ..f_slice_2 = f.iloc[:, 11:13]# 12 13# 0 x x# 1 x x# 2 x x# ..
10-01 13:03