SoFunction
Updated on 2024-11-20

Python data analysis numpy text data reading index slicing example details

Data import and array transposition

(framme,dtype='dataType',delimmiter='delimiter',

skiprows=''(number of rows skipped'),

usecols=''Number of lines to be used',''

unpack='Ture/Flase(transpose or not)':Load text file data

Meaning of loadtxt parameter

  • The numpy array transpose is done in 4 ways
    • The value of the parameter unpack is set to TRUE.
    • Transpose using the .T property of the array
    • Transpose using the transpose() method of an array
    • Using the swapaxes method with numpy arrays

Examples are as follows:

import numpy as np
filepath = './'
t1 = (filepath,usecols=(1,2,3),delimiter=',',dtype='float')
print(t1)
# Four ways to transpose
# first method:Set the value of parameter "unpack" —— True
t2 = (filepath,usecols=(1,2,3),delimiter=',',dtype='float',unpack=True)
# second method: use the '.T' attributions of array's
t3 = 
print(t3)
# third method: use the method of 'transpose'
t4 = ()
print(t4)
# forth method: swapaxes(arguments:axes needed swapped)
t5 = (0,1)
print(t5)

Run results:

numpy array indexing and slicing

import numpy as np
filename = './'
t1 = (filename,delimiter=',',dtype='float',usecols=(1,2,3))
# print(t1)
# Fetch line operations
print(t1[0])
print(t1[0,:])
# Take multiple consecutive lines
print(t1[3:])
print(t1[3:,:])
# Fetch discrete multiple lines
print(t1[[1,3,13,19]])
print(t1[[1,2,4,6],:])
# Fetch column
print(t1[:,0])
# Take consecutive columns
print(t1[:,2:])
# Take discontinuous columns
print(t1[:,[1,2]])
# Take rows 2-5, columns 2-3
# Fetch cross data from multiple locations
print(t1[1:5,1:3])
# Fetch data information from non-adjacent locations
print(t1[[1,4,6],[0,1,2]])
import numpy as np
filepath = './'
t1 = (filepath,delimiter=',',usecols=(1,2,3))
print(t1<9.5)
t1[t1 < 9.5] = 0
print(t1[:,1])
# if-else operations
(t1>=9.6,10,0)
print(t1)
# clip(m,n)Putting an array with less thanm's replacement withm,more thann's replacement withn

The above is Python data analysis numpy text data reading index slicing example details, more information about Python numpy data reading index please pay attention to my other related articles!