A data type that can store multiple data, similar to lists and tuples. It has the following features.
{}
.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.
A data type that can store multiple data as well as lists, tuples, and sets. It has the following features.
{Label 1: Data 1, Label 2: Data 2, Label 3: Data 3}
.keys ()
gives a list of labels and ʻitems ()` gives a list of values.In lists and tuples, multiple data are specified by indexes, but in dictionaries, they are specified by labels.
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])
Lists, tuples, and sets can be converted to each other using the methods list ()
, tuple ()
, and set ()
, respectively.
# 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]
The basic data type used by Numpy
in a typical numerical calculation package.
# 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
DataFrame
type (represents table data)Seriez
type (representing row or column data)Create as follows.
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.
r.colums = ['C1', 'C2']
r.index = ['A', 'B', 'C']
As with the index type, you can get column data by specifying a subscript.
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