Python beginner's note

Reference article (comprehensive and systematic summary)

pandas pandas.DataFrame pandas.DataFrame.sample

pandas.Series pandas.Series.value_counts

vc = df['state'].value_counts()
print(vc)
print(type(vc))
# NY    2
# CA    2
# TX    1
# Name: state, dtype: int64
# <class 'pandas.core.series.Series'>

str str.replace

s = 'one two one two one'
print(s.replace(' ', '-'))
# one-two-one-two-one
s = 'one two one two one'
print(s.replace('one', 'XXX').replace('two', 'YYY'))
# XXX YYY XXX YYY XXX

Replacement by slice

s = 'abcdefghij'
print(s[:4] + 'XXX' + s[7:])
# abcdXXXhij

str.join

test = ['ab', 'c', 'de']
result = ''.join(test)
print(result)
# abcde4

str.split

str.ljust, str.rjust, str.center

s = 'abc'
s_rjust = s.rjust(8)
print(s_rjust)
#      abc
print(s.center(8, '+'))
# ++abc+++

all

print(all([True, True, True]))
# True

any

print(any([True, False, False]))
# True

find, rfind

s = 'I am Sam'
print(s.find('Sam'))
# 5
print(s.find('XXX'))
# -1

list

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a[::-1])
# [9, 8, 7, 6, 5, 4, 3, 2, 1]

"[]" Is removed when * list is output with *

Comprehension + conditional expression

Comprehension + nest

List=[1*i + 10*j + 100*k for k in range(2) for j in range(3) for i in range(4)]
print(List)
# [0, 1, 2, 3, 10, 11, 12, 13, 20, 21, 22, 23, 100, 101, 102, 103, 110, 111, 112, 113, 120, 121, 122, 123]
init=[[1,2,3],[4,5],[6,7]]
[inner for outer in init for inner in outer]
# [1, 2, 3, 4, 5, 6, 7]

Delete element

list.pop

List=["a","b","c","d","e","f"]
print(List.pop(1))
# b
print(List)
# ['a', 'c', 'd', 'e', 'f']

list.remove

List=["a","b","c","d","e","f"]
List.remove('d')
print(List)
# ['a', 'b', 'c', 'e', 'f']

del

List=["a","b","c","d","e","f"]
del List[1]
print(List)
# ['a', 'c', 'd', 'e', 'f']

list.insert

List=["a","b","c"]
List.insert(1,'z')
print(List)
# ['a', 'z', 'b', 'c']

set

l = [2, 2, 3, 1, 3, 4]
l_unique = list(set(l))
print(l_unique)
# [1, 2, 3, 4]
s1 = set([1, 2, 3])
s2 = set([2, 3, 4])
print(s1 | s2)
# {1, 2, 3, 4}

ord

ord("a")
# 74

chr

chr(97)
# a

print

a,b,c,d = map(int, input().split())
print('TAKAHASHI' if b/a > d/c else 'AOKI' if b/a < d/c else 'DRAW')

float float.is_integer

f_i = 100.0
print(f_i.is_integer())
# True

numpy numpy.rehape

mask[:, :, idx] = mask_label.reshape(256, 1600, order='F')

numpy.nditer

numpy.where

a = np.arange(20, 0, -2)
print(a)
# [20, 18, 16, 14, 12, 10,  8,  6,  4,  2]

print(np.where(a < 10))
# [6, 7, 8, 9]

bisect

A = [1, 2, 3, 3, 3, 4, 4, 6, 6, 6, 6]
print(A)
index = bisect.bisect_left(A, 3)
#2 (far left(Before)The insertion point of is returned)

eval

print(eval('1 + 2'))
# 3

reduce

a = [-1, 3, -5, 7, -9]
print reduce(lambda x, y: x + y, a)
# -5

map, filter

tqdm.tqdm

from tqdm import tqdm
for _ in tqdm(range(100)):
  time.sleep(0.1)

Path.pathlib

from pathlib import Path
train_path = Path("../input/train_images/")
for img_name in train_path.iterdir():
    img = Image.open(img_name)

seaborn

import seaborn as sns
sns.barplot(x=list(class_dict.keys()), y=list(class_dict.values()), ax=ax)

glob.glob

import glob
glob.glob('*.log')
# ['abc.log', 't_1.log', 't_2.log']
glob.glob(’t_*.log’)
# [t_1.log, t_2.log]

Recommended Posts

Python beginner's note
Note: Python
Python note
Python study note_002
Note: Python Decorator
Python programming note
[Python] Learning Note 1
Python study note_004
Markdown Beginner's Note
Python study note_003
[Note] openCV + python
Beginners practice Python
Python Beginner's Guide (Functions)
[Note] future sentence ~ Python ~
[Note] File reading ~ Python ~
Python beginners organize heapsort
Python beginners organize quicksort
Python beginners touch Pytorch (3)
python textbook for beginners
Note: python Skeleton Nya
Python basic grammar note (4)
Python basic grammar note (3)
Python beginners touch Pytorch (2)
Python Beginner's Guide (Introduction)
OpenCV for Python beginners
Note
Python
About python beginner's memorandum function
Python Input Note in AtCoder
[Note] Operate MongoDB with Python
Note
Python3 environment construction (for beginners)
3 Reasons Beginners to Start Python
Python beginners organize bubble sort
[WIP] Fluent Python Study Note
Basic Python grammar for beginners
3 months note for starting Python
100 Pandas knocks for Python beginners
[AtCoder] ABC165C Personal Note [Python]
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
Note that it supports Python 3
A note about [python] __debug__
[Note] Project Euler in Python (Problem 1-22)
Python Exercise for Beginners # 2 [for Statement / While Statement]
Machine learning summary by Python beginners
(Note) Be careful with python argparse
Python #index for super beginners, slices
[Note] Hello world output with python
Typing automation notes by Python beginners
boto3 (AWS SDK for Python) Note
<For beginners> python library <For machine learning>
Python #len function for super beginners
Foreign Key in Python SQLite [Note]
Beginners use Python for web scraping (1)
Run unittests in Python (for beginners)
Memorandum of beginners Python "isdigit" movement
Beginners use Python for web scraping (4) ―― 1
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners