Python 入门练习--读取数据,并将某几列的数据赋值给变量

There are many commands that can load data into Python. Here I use genfromtxt and loadtxt as example.

1 import numpy as np
2 my_data = np.genfromtxt('name_of_the_data', skip_header=10) # skip the first 10 rows of the data, and the true data is loaded from line 11

or

1 import numpy as np
2 my_data = np.loadtxt('before.dump',skiprows=10) # skip the first 10 rows of the data, and the true data is loaded from line 11
1 x = my_data[:,3]  # extract x from data, assign data we need to x 
2 y = my_data[:,4]  # the colon represents all values. [:,4] means all the values of column 5
3 z = my_data[:,8]  #                                  [3,:] means all the values of row 4

~~~~~~

1 # Get help of these two commands
2 help(np.loadtxt)
3 help(np.genfromtxt)

 

posted @ 2020-09-26 09:33  cfdchen  阅读(2148)  评论(0编辑  收藏  举报