Python NumPy Pandas

Source: Internet
Author: User

Import numpya=numpy.array ([1,2,3,4]) b=numpy.array ([[1,2,3],[4,5,6],[7,8,9]])print (A.shape) Print (B.shape)

Creates a one-dimensional vector and a matrix of three rows of hashes

Note: Here the data is required to be the same structure, the shape function: Several rows of columns

Value:

Import numpyb=numpy.array ([[[1,2,3],[4,5,6],[7,8,9]])print (b[:,1])# Here we print the second column of the Matrix  Print(B[:,0:2])# This takes you to the first and second columns

To modify a value in a matrix:

This changes the value of 5 and 7 to 10.

Import numpyb=numpy.array ([[[1,2,3],[4,5,6],[7,8,9]]) b[(b==5) | ( b==7)] = tenprint(b)

Strong Turn Type:

convert int to str type

Import numpyb=numpy.array ([[[1,2,3],[4,5,6],[7,8,9 = b.astype (str)print(c)

Other operations:

Import numpyb=numpy.array ([[[1,2,3],[4,5,6],[7,8,9]])print(B.min ())# to find the minimum value  Print(B.max (Axis=1))# max print(b.sum (Axis=0)) # by line# Sum by Column

Import NumPy as NPA=np.arange (0). Reshape (2,5)print(a)" creates a matrix: [[2] + 3 4] [5 6 7 8 9]] " " Print (A.ndim) # Seeking Dimensions Print (A.shape) # a few rows of columns Print (A.dtype.name) # matrix data type name Print (a.size) # Number of elements

Matrix initialization:

Import NumPy as NP # Matrix initialization method:Np.zeros ((3,4))#3 row 4 column matrix initialized to 0 (default to float type)np.ones ((3,4), Dtype=np.int32)#  3 row 4 column initialize int type with value 1

To create a matrix:

Import NumPy as Npnp.arange (10,30,5) # from 10 to 30, every 5 # Array ([ten, +,]) Np.random.random ((2,3)) " " randomly created: 2 rows 3 columns, 1 to 1 note: Must be two randomarray ([[0.20925672, 0.09790786, 0.00158854],       [0.73711854, 0.83033327, 0.22525092]]"np.linspace (1,3,100)#  Averaging 100 numbers from 1 to 3 (float type)

Operation:

ImportNumPy as NPA=np.array ([[1,2,3],[4,5,6],[7,8,9]])Print(Np.hstack ((a,a)))Print(Np.vstack ((a,a)))Print(A.T)Print(A +a)Print(A *a)Print(A.dot (a))Print(Np.dot (a,a))Print(Np.exp (a))Print(Np.sqrt (a))Print(A.shape)Print(A.ravel ())" "Do not explain, at a glance [[1 2 3 1 2 3] [4 5 6 4 5 6] [7 8 9 7 8 9]][[1 2 3] [4 5 6] [7 8 9] [1 2 3] [4 5] 6 7] (8 4 7) [3 6 9]] [[2 4 6] [8 10 12] [14 16 18]] [[1 4 9] [16 25 36] [49 64 81]] [[30 36 42] [66 81 96] [102 126 150]]  [[30 36 42] [66 81 96] [102 126 150]] [[2.71828183e+00 7.38905610e+00 2.00855369e+01] [5.45981500e+01 1.48413159e+02 4.03428793e+02] [1.09663316e+03         2.98095799E+03 8.10308393e+03]] [[1].         1.41421356 1.73205081] [2.        2.23606798 2.44948974] [2.64575131 2.82842712 3. ] (3, 3) [1 2 3 4 5 6 7 8 9]" "

Import NumPy as NPA=np.array ([[[1,2,3],[4,5,6],[7,8,9]])print(A.argmax (axis=0))#  [2 2 2] column maximum index value print(a.argmin (Axis=1))#[0 0 0] Row min index value

Import NumPy as NPA=np.arange (0,40,10)print(a) b=np.tile (A, (3,2)) C=np.tile ( A, (2,3))print(b)print(c)"[0 10 20 30][[0 10 20 30<  c13/>0 [0] [0] [0] [0] [  [0  ] 0 0 (10)----+------+------ [0 0] [  0] []] ""

Sort:

ImportNumPy as NPA=np.array ([[1,4,6],[2,9,7],[5,3,8]])Print(a)" "[ [1 4 6] [2 9 7] [5 3 8]]" "b=np.sort (A,axis=1)#Arrange By RowPrint(b)" "[ [1 4 6] [2 7 9] [3 5 8]]" "C=np.sort (a,axis=0)#Arrange By ColumnPrint(c)" "[ [1 3 6] [2 4 7] [5 9 8]]" "D=np.argsort (a)#index Value OrderingPrint(d)" "[ [0 1 2] [0 2 1] [1 0 2]]" "

Special attention:

ImportNumPy as NPA=np.array ([[1,2,3],[4,5,6],[7,8,9]]) C=A.view ()Print(c isA#False (C and a point to memory addresses are different)#copy A, assign a value to C#if it is c=a, then C and a are the same (point to the same address)#Print (c is a) in the word, it prints truec[1,2] = 100Print(a)" "[ [1 2 3] [4 5] [7 8 9]]" "#here we find that C has been modified, so a has also been modified.#C and a have different addresses but share a set of dataD=a.copy ()Print(d isA#falsed[1,3] = 100#There's no change here .Print(a)

Read TXT file:

Import NumPy # The first parameter is a path, the second argument is a delimiter, and the third argument is the type of the read # The last parameter means: Do you want to remove the first line a=numpy.genfromtxt ("d:/a.txt", delimiter=",  ", dtype="str", skip_header=1)print(a)

Pandas for data processing:

Examples of Use:

ImportPandasfood= Pandas.read_csv ("D:/a.csv")#Read CSV filePrint(food.dtypes)#field TypePrint(Food.head (4))#get first 4 rows (default = 5)Print(Food.tail (3))#get the following 3 rows (default = 5)Print(Food.shape)#a few rows of columnsPrint(food.columns)#each column name
Print (food.loc[1]) # get the 2nd row of data Print (food["name"]) # get name to column

1.

ImportPandasfood= Pandas.read_csv ("D:/a.csv") List=food.columns.tolist ()Print(list)#convert all column names to listsList1= [] forCinchlist:if(C.endswith ("(MG)")): List1.append (c) a=Food[list1]Print(a)#Add a new list to the end of (MG) and process the complete

2. Sort (default ascending)

Import= pandas.read_csv ("d:/a.csv") food.sort_values ("  Calcium_ (mg)", Inplace=true, ascending=False)# in descending order, first parameter column name, The third argument, whether the third one is ascending, and the default is Trueprint(food["calcium_ (mg)"))

3.

ImportPandasman= Pandas.read_csv ("D:/t.csv")Print(man) age= man[" Age"]#Age ColumnAge_null = Age[pandas.isnull (man[" Age"])]#field age is empty lineAge_null_len =Len (age_null)#The sum of age is empty

Python NumPy Pandas

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.