如何使用 python 导入标题完整的 csv 文件,其中第一列是非数字
这是对上一个问题的详细阐述,但随着我深入研究 python,我对 python 如何处理 csv 文件感到更加困惑。
我有一个 csv 文件,它必须保持这种状态(例如,无法将其转换为文本文件)。它相当于 5 行 x 11 列的数组或矩阵或向量。
我一直在尝试使用我在这里和其他地方(例如python.org
)找到的各种方法读取csv,以便它保留列和行之间的关系,其中第一行和第一行列 = 非数字值。其余的都是浮点值,并且包含正浮点和负浮点的混合。
我想要做的是导入 csv 并在 python 中编译它,这样如果我要引用列标题,它将返回存储在行中的关联值。例如:
>>> workers, constant, age
>>> workers
w0
w1
w2
w3
constant
7.334
5.235
3.225
0
age
-1.406
-4.936
-1.478
0
等等...
我正在寻找处理这种数据结构的技术。我对 python 很陌生。
This is an elaboration of a previous question, but as I delve deeper into python, I just get more confused as to how python handles csv files.
I have a csv file, and it must stay that way (e.g., cannot convert it to text file). It is the equivalent of a 5 rows by 11 columns array or matrix, or vector.
I have been attempting to read in the csv using various methods I have found here and other places (e.g. python.org
) so that it preserves the relationship between columns and rows, where the first row and the first column = non-numerical values. The rest are float values, and contain a mixture of positive and negative floats.
What I wish to do is import the csv and compile it in python so that if I were to reference a column header, it would return its associated values stored in the rows. For example:
>>> workers, constant, age
>>> workers
w0
w1
w2
w3
constant
7.334
5.235
3.225
0
age
-1.406
-4.936
-1.478
0
And so forth...
I am looking for techniques for handling this kind of data structure. I am very new to python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于 Python 3
删除
rb
参数并使用r
或不传递参数(默认读取模式
) 。对于 Python 2
Python 有一个强大的内置 CSV 处理程序。事实上,大多数东西已经内置在标准库中。
For Python 3
Remove the
rb
argument and use eitherr
or don't pass argument (default read mode
).For Python 2
Python has a powerful built-in CSV handler. In fact, most things are already built in to the standard library.
Python 的 csv 模块按行处理数据,这是查看此类数据的常用方式。您似乎想要一种按列的方法。这是一种方法。
假设您的文件名为
myclone.csv
并包含此代码,应该会给您一个或两个想法:
要将数值转换为浮点数,请
在前面添加此内容,并对
每一行执行此操作,而不是类似的上面两行。
Python's csv module handles data row-wise, which is the usual way of looking at such data. You seem to want a column-wise approach. Here's one way of doing it.
Assuming your file is named
myclone.csv
and containsthis code should give you an idea or two:
To get your numeric values into floats, add this
up front, and do this
for each row instead of the similar two lines above.
您可以使用 pandas 库并引用行和列,如下所示:
You can use pandas library and reference the rows and columns like this:
我最近不得不为相当大的数据文件编写这个方法,我发现使用列表理解效果很好
I recently had to write this method for quite a large datafile, and i found using list comprehension worked quite well