【发布时间】: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