简体   繁体   English

如何使用 python 和 Z3A43B4F88325D24022C0EFA9C 库从 csv 文件导入二维 arrays?

[英]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.

相关问题 我可以将二维 Arrays 保存到 python 中的文本文件吗 - Can i save Two-Dimensional Arrays to text file in python 如何使用两个随机变量列表在python中创建点列表(二维数组) - How can I create a list of points (two dimensional arrays) in python using two lists of random variables 使用python将二维数组转换为csv文件 - two dimensional array to csv file using python 如何将 csv 文件中的特定列导入 python? - How can I import specific columns from csv file to python? 如何使用python中的pandas从csv文件读取? - How do I read from a csv file using pandas in python? 如何使用Matlab数据结构中的多维数组从Python创建Matlab文件? - How can I create a Matlab file from Python with multi-dimensional arrays in a Matlab data structure? Python Pandas:我可以使用动态文件路径通过用户输入导入 CSV 吗? - Python Pandas: Can I import a CSV through user input using a dynamic file path? 如何使用 pandas 从 csv 文件中删除一行? - How can I remove a line from a csv file using pandas? 如何使用 Z3A43B4F88325D94022C0EFA 库在 python 的 2 列 CSV 文件上更改 header 而不创建新的 C9 文件? - How do I change the header on a 2 column CSV file in python using the pandas library without creating a new file? 如何使用python比较两个不同的csv文件? - How can I compare two different csv file using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM