#generate list
list = [0,1,2]
print(list)
[0, 1, 2]
#Add list
list2 = [[0,1,2]]
list2.append([3,4,5])
list2
[[0, 1, 2], [3, 4, 5]]
#list
len([0,1,2])
3
#list
[0,1,2].count(0)
1
#dictionary{key:value}Created in the format of
#Returns value when specified in method get
{"Miyasako" : "Honest", "Tamura" : "return"}.get("Miyasako")
'Shinshin'
#list specified in slice
#[:3]Specify from 0 to the third before
[0, 1, 2, 3, 4, 5][:3]
[0,1,2]
#How to make a UDF function
def printHello():
return print("Hello, world!!")
printHello()
'Hello, world!!'
#Conditional branch in if statement
x = 10
if x <= 20:
print("x is 20 or less.")
if x <= 30:
print("x is 30 or less.")
elif x >=40:
print("x is 40 or more.") #This sentence does not appear
'x is 20 or less. ' 'x is 30 or less. '
#for statement
x = 0
for i in range(100):
x += i
4950
numpy module import
#Import numpy module
import numpy as np
example1 = np.array([2, 4, 6, 8, 10])
example1
array([ 2, 4, 6, 8, 10])
#print("{}".format())Can be commented so it is easy to check the contents
print("example1:\n{}".format(example1))
example1: [ 2 4 6 8 10]
#If you pass a nested list
example2 = np.array([[1, 2, 3, 4, 5], [2, 4, 6, 8, 10]])
example2
array( [[ 1, 2, 3, 4, 5], [ 2, 4, 6, 8, 10]])
#Dimension and shape of example2
#Note that shape cannot be used for list! Let's put it in ndarray
print(example2.ndim, example2.shape)
2 (2, 5)
#Matrix permutation
example2.transpose()
array( [[ 1, 2], [ 2, 4], [ 3, 6], [ 4, 8], [ 5, 10]])
#ndarray → list conversion
np.array([1,2,3,4,5][2,3,4,5,6]).tolist()
pandas module import
#Import pandas module
import pandas as pd
data = pd.DataFrame({"area" : [18, 19, 20], "age" : [5, 4, 10]}, index = ["A", "B", "C"])
data
#Refer to only the first n lines of the data frame.
data.head(1)
#Numpy array of values from the dataframe/Get a numpy array of column names.
data.values, data.columns
(array([ [ 5, 18], [ 4, 19], [10, 20]]), Index(['age', 'area'], dtype='object'))
#Summary statistics
data.describe()
#Correlation coefficient
data.corr()
#Delete column
data = data.drop("time", axis = 1)
data
#Column selection
data[["age","area"]]
#Condition specification
data[(data["age"]<=5) & (data["area"]==18)]
# ndarray→dataframe
A = np.array([[1,2],[2,3],[3,4]])
#cloumns naming
B = pd.DataFrame(A,columns={"NO_1","NO_2"})
#columns renamed
B.rename(columns={"NO_1":"no1","NO_2":"no2"})
#Aggregate
B.groupby("NO_1",as_index=False).sum()
Recommended Posts