[英]How can i import two dimensional arrays from a csv file using python and the pandas library?
Im working on a project that requires i import data from a csv file what ive tried looks like this:我正在处理一个需要我从 csv 文件中导入数据的项目,我尝试过的看起来像这样:
code:代码:
import Pandas as pd
a_xyz = []
df = pd.read_csv('parachute jump.csv', sep = ';', decimal = '.', comment = '#')
a_xyz_mg= df['a_values'].tolist()
the csv file looks like this: csv 文件如下所示:
[x1, y1, z1]
[x2, y2, z2]
[x3, y3, z3]
I want the output to be a two dimensional array that looks like this:我希望 output 是一个二维数组,如下所示:
a_xyz = [[x1, y1 z1], [x2, y2, z2], [x3, y3, z3]]
and if i ask for x1 like this:如果我这样要求 x1:
a_xyz[0][0]
i want the output to be x1 Currently the output is我希望 output 是 x1 目前 output 是
print(a_xyz[0][0])
= x
meaning i get the first letter not the first element.意思是我得到第一个字母而不是第一个元素。 Also, the objects are put in '', making it hard to make them float此外,对象被放在''中,很难让它们漂浮
#Any help would be greatly appreciated!! #任何帮助将不胜感激!!
this can be done without pandas using list comprehension这可以在没有 pandas 的情况下使用列表理解来完成
data = open('parachute jump.csv', 'r').read().split('\n')
data = [[d for D.split(';')] for D in data]
if you want d to be an integer or a float then use int(d) or float(d)如果您希望 d 成为 integer 或浮点数,则使用 int(d) 或 float(d)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.