gpt4 book ai didi

python - 如何在 linux 中安装 csvformat?

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:01 25 4
gpt4 key购买 nike

我以前在 Raspberry Pi 上工作,但是一旦我转移到 Ubuntu PC 上,下面的代码就不再工作了。我想我需要以某种方式安装该 csvformat,但不确定如何安装。

Python代码:

import os
import subprocess
import pandas
import pandas as pd

subprocess.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 found
Traceback (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]

最佳答案

您有 2 个不同的、不相关的问题。

第一个问题:

/bin/sh: 1: csvformat: not found

您的脚本似乎在寻找 csvformat命令行工具,属于 csvkit 的一部分.

您可以 install csvkit with pip :

1.2. Installing csvkit

Installing csvkit is easy:

sudo pip install csvkit

Note:

If you’re familiar with virtualenv, it is better to install csvkit in its own environment. If you are doing this, then you should leave off the sudo in the previous command.

如 csvkit 文档(和评论)所述,执行 sudo pip install 通常被认为是错误的。参见 What are the risks of running 'sudo pip'? .使用虚拟环境是推荐的方法。

安装 csvkit 后,检查您现在是否拥有 csvformat:

csvformat -h

现在,当您运行脚本时,请确保您用于安装 csvkit 的 pip相同的 Python(pythonpython3, ..) 用于运行脚本。例如,如果您使用 python 运行您的脚本:

$ python -m pip list | grep csvkit
csvkit 1.0.4

如果 csvkit 没有出现,那么你将它安装在其他地方。

第二个问题:

KeyError: "None of [Int64Index([11, 13], dtype='int64')] are in the [columns]

你得到这个是因为你不正确地访问(切片)f 的内容。 read_csv的归还是 Pandas 数据框。您不能只使用 f[11,13] 对其进行切片。您需要使用 loc (基于标签的切片)或 iloc (基于整数索引的切片)。

我不知道你实际上想用 keep_col = [11,13] 做什么,但你应该这样做:

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
# ..

关于python - 如何在 linux 中安装 csvformat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56990099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com