【问题标题】:How to import an array of arrays from CSV in Python如何在 Python 中从 CSV 导入数组数组
【发布时间】:2019-06-29 10:45:02
【问题描述】:

所以基本上我是 Python 新手,有些事情我无法做到。我正在从 CSV 导入数据,我需要我的 data_2d 看起来像这样:

data_2d = [ [30, 15, 0, 15, 0, 0, 0],
        [32, 10, 0,10, 3, 5, 0],
        [5, 9, 0, 25, 10, 8, 3],
        [22, 10, 0  ,17, 5, 6, 0],
        [7, 15, 0, 30, 3, 5, 0]]

相反,使用我当前的代码,我得到了:

[['30' '15' '0' '15' '0' '0' '0']
['32' '10' '0' '10' '3' '5' '0']
['5' '9' '0' '25' '10' '8' '3']
['22' '10' '0' '17' '5' '6' '0']
['7' '15' '0' '30' '3' '5' '0']]

我的代码在这里:

data_2d = [ [30, 15, 0, 15, 0, 0, 0],
        [32, 10, 0,10, 3, 5, 0],
        [5, 9, 0, 25, 10, 8, 3],
        [22, 10, 0  ,17, 5, 6, 0],
        [7, 15, 0, 30, 3, 5, 0]]

data_2d = []

with open('file.csv', newline='\n') as f:
    reader = csv.reader(f, delimiter=';')
    for row in reader:
        data_2d.append(row)

data_array = np.array(data_2d)
data_array = np.delete(data_array, (0), axis=0)
data_array = np.delete(data_array, (0), axis=1)

print("data_array")
print(data_array)

CSV 文件目前如下所示:

Time_Activity;SITTING;STANDING;LAYING;WALKING;WALKING_DOWNSTAIRS;WALKING_UPSTAIRS;RUNNING
8;30;15;0;15;0;0;0
9;32;10;0;10;3;5;0
10;5;9;0;25;10;8;3
11;22;10;0;17;5;6;0
12;7;15;0;30;3;5;0

【问题讨论】:

    标签: python arrays python-3.x csv numpy


    【解决方案1】:

    要立即修复,请使用astype 方法将字符串数组转换为int

    data_array = data_array.astype(int)
    

    另外,np.delete效率低,不推荐;尽可能使用切片。以下是您可以完全避免 Python 级循环的几种方法:-

    numpy

    使用 NumPy,您可以使用np.genfromtxt

    arr = np.genfromtxt('file.csv', delimiter=';', skip_header=1)[:, 1:]
    

    pandas

    或者,您可以通过 Pandas 使用pd.read_csv

    arr = pd.read_csv('file.csv', sep=';').iloc[:, 1:].values
    
    print(arr)
    
    # array([[30, 15,  0, 15,  0,  0,  0],
    #        [32, 10,  0, 10,  3,  5,  0],
    #        [ 5,  9,  0, 25, 10,  8,  3],
    #        [22, 10,  0, 17,  5,  6,  0],
    #        [ 7, 15,  0, 30,  3,  5,  0]], dtype=int64)
    

    【讨论】:

      【解决方案2】:

      csv 文件被读取为字符串。您可以在追加时将行从字符串转换为 int。这可以使用map 函数来完成。

      代码:

      data_2d.append(list(map(int,row)))
      

      【讨论】:

        【解决方案3】:

        你在正确的轨道上。有两件事可以帮助您实现目标并稍微简化代码。

        1. 跳过标题,这样您就不必担心以后会删除它。
        2. 在追加之前将字符串转换为 int。

        适用于您的输入的示例代码:

          data_2d = []
          with open('file.csv', newline='\n') as f:
            reader = csv.reader(f, delimiter=';')
            next(reader) # this will skip that header
            for row in reader:
              data_2d.append([int(x) for x in row]) #this just means take each string and make it an int before appending.
        

        结果:

        [[8, 30, 15, 0, 15, 0, 0, 0], 
        [9, 32, 10, 0, 10, 3, 5, 0], 
        [10, 5, 9, 0, 25, 10, 8, 3], 
        [11, 22, 10, 0, 17, 5, 6, 0], 
        [12, 7, 15, 0, 30, 3, 5, 0]]
        

        一些有用的链接解释了这两个添加: https://evanhahn.com/python-skip-header-csv-reader/ https://www.pythonforbeginners.com/basics/list-comprehensions-in-python

        【讨论】:

          猜你喜欢
          • 2014-04-10
          • 2016-07-24
          • 2019-12-05
          • 1970-01-01
          • 2021-06-13
          • 2018-03-18
          • 2014-09-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多