[PYTHON] Learning record (6th day) #Set type #Dictionary type #Mutual conversion of list tuple set #ndarray type #Pandas (DataFrame type)

content of study

Set type

A data type that can store multiple data, similar to lists and tuples. It has the following features.

Description example
a = {1,0,2,9,8,3,7,5,4,6}
print('a = ',a)

b = {2,4,4,6,5,2,1,0,8,7,9,3,6}
print('b = ', b)

Execution result

a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} b = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

No matter what order you specify the data like the execution result, it will be sorted and output.

Dictionary type

A data type that can store multiple data as well as lists, tuples, and sets. It has the following features.

In lists and tuples, multiple data are specified by indexes, but in dictionaries, they are specified by labels.

Description example
c = {'l1':12, 'l2':45, 'l3':36, 'l4':58}

print('c = ', c)
print("c['l2'] = ", c['l2'])
print("c['l4'] = ", c['l4'])
print(c.keys())
print(c.values())

Execution result

c = {'l1': 12, 'l2': 45, 'l3': 36, 'l4': 58} c['l2'] = 45 c['l4'] = 58 dict_keys(['l1', 'l2', 'l3', 'l4']) dict_values([12, 45, 36, 58])

Mutual conversion of lists, tuples and sets

Lists, tuples, and sets can be converted to each other using the methods list (), tuple (), and set (), respectively.

Description example
# A program that uses mutual conversion to remove duplicate data from a list
d = [0,2,6,4,2,8,0,2]

e = list(set(d))
print('e = ', e)

Execution result

e = [0, 2, 4, 6, 8]

ndarray type

The basic data type used by Numpy in a typical numerical calculation package.

Description example
# Creating ndarray type
import numpy

f = [0, 1, 2, 3]
 g = numpy.array (f) # Create ndarray type from list
print('g = ', g)

h = [1, 2]
i = [2, 3]
j = [3, 4]
 k = numpy.array ([h, i, j]) # Create a two-dimensional ndarray type
print('k = ', k)

 l = numpy.array (a, dtype = numpy.float16) #Create ndarray by specifying data type
print('l = ', l)

Execution result

g = [0 1 2 3] k = [[1 2] [2 3] [3 4]] l = [0. 1. 2. 3.]

In addition, various numerical operations can be performed on ndarray type data and lists by using the functions provided by Numpy. The representative ones are introduced below.

import numpy

m = numpy.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

 n = numpy.median (m) # median
 o = numpy.mean (m) #mean
 p = numpy.std (m) # standard deviation
 q = numpy.var (m) # Distributed

print(n)
print(o)
print(p)
print(q)

Execution result

4.5 4.5 2.8722813232690143 8.25

Pandas

Basic data type

DataFrame type

Create as follows.

Description example
import pandas

r = pandas.DataFrame([1, 11.1], [2, 22.2], [3, 33.3])

You can specify column and row subscripts with column and ʻindex`, respectively.

Description example
r.colums = ['C1', 'C2']
r.index = ['A', 'B', 'C']

As with the index type, you can get column data by specifying a subscript.

Description example
 s = r ['C1'] # Extract only the data in the first column
 t = s ['A'] # Extract the data in the first column and the first row

Recommended Posts

Learning record (6th day) #Set type #Dictionary type #Mutual conversion of list tuple set #ndarray type #Pandas (DataFrame type)
Basic operation list of Python3 list, tuple, dictionary, set
Learning record 4 (8th day)
Learning record 3 (7th day)
Learning record 5 (9th day)
Learning record 6 (10th day)
Learning record 8 (12th day)
Learning record 1 (4th day)
Learning record 7 (11th day)
Learning record 2 (6th day)
Learning record 16 (20th day)
Learning record 22 (26th day)
Summary of Python sort (list, dictionary type, Series, DataFrame)
Learning record No. 21 (25th day)
Learning record 13 (17th day) Kaggle3
Learning record No. 10 (14th day)
Learning record 12 (16th day) Kaggle2
Learning record No. 24 (28th day)
Learning record No. 23 (27th day)
Learning record No. 25 (29th day)
Learning record No. 26 (30th day)
Learning record No. 20 (24th day)
Learning record No. 14 (18th day) Kaggle4
Learning record No. 15 (19th day) Kaggle5
Learning record 11 (15th day) Kaggle participation
To speed up python, summarize the amount of calculation of collection type (list / tuple / dictionary / set) for each purpose.
Type conversion of multiple columns of pandas DataFrame with astype at the same time