LoginSignup
1
0

More than 1 year has passed since last update.

kaggleで勝つためにpython文法の弱いところを補強してみた

Last updated at Posted at 2021-08-03

使用書籍 独学プログラマー

オブジェクトとは

  • pythonにおける「オブジェクト」とは、同一性、データ型、値の3つの要素がある
  • 同一性:コンピュータのメモリのどこに格納されているかで決まり、変化しない
  • データ型:データがどんな性質を持っているかグループ分けしたもの。データ型が持つ性質は変化しない
  • 値:それが表すデータのこと
    • "hello world"は、hello worldというstrデータ型の値を持つオブジェクト。

関数

def f(x):
    return x + 1
z = f(5)
if z == 5:
    print('z is 5')
else:
    print('z is not 5')
def f():
    return 1 + 1
result = f()
if result == 5:
    print('result is 5')
else:
    print('result is not 5')
result is not 5
def f(x, y, z):
    return x + y + z + 1
z = f(5, 4, 3)
if z == 5:
    print('z is 5')
else:
    print('z is not 5')
    print('z is ', z)
z is not 5
z is  13

組み込み関数

  • 最初から用意されている関数
  • あらゆる種類の計算とタスクを実行できて、特別な準備なしでもすぐに使用できる
print('hello')
hello
len('hello')
5
print('sum is ', str(100))
sum is  100
a = 100
b = int('200')
c = a + b
d = a + b + float(c)
print(c)
print(d)
300
600.0
age = input('enter your age')
int_age = int(age)
if int_age <= 22:
    print('you are student')
else:
    print('you are business man')
enter your age23
you are business man

関数を再利用する

def even_odd():
    a  = input('type a number')
    x = int(a)
    if x % 2 == 0:
        print('even!')
    else:
        print('odd!')

even_odd()

type a number43
odd!

必須引数とオプション引数

def f(x=2):
    return x * x
print(f())       #デフォルトの値が使用された
print(f(x = 5))  #デフォルトの値が上書きされて渡した数値が使用された
4
25
def add_it(x, y =  12):
    return x + y
add_it(x = 10)
# xには値を渡して、yにはデフォルト値を設定
22

スコープ

a = 1
b = 2
c = 3
# グローバル変数
def f():
    print(a)
    print(b)
    print(c)
f()
1
2
3

globalキーワード

x = 100 # global

def f():
    x = 100 # local
    x += 1
    print(x)
f()
print(x)
101
100
x = 100

def f():
    global x # edit x in local env
    x += 1
    print(x)
f()
print(x)
101
101

例外処理

a = input('type a number: ')
b = input('type a number: ')
a = int(a)
b = int(b)
print(a / b)
type a number: 3
type a number: 0



---------------------------------------------------------------------------

ZeroDivisionError                         Traceback (most recent call last)

<ipython-input-66-5e467544e423> in <module>
      3 a = int(a)
      4 b = int(b)
----> 5 print(a / b)


ZeroDivisionError: division by zero
#上記の問題をコードで解決する
a = input('type a number: ')
b = input('type a number: ')
a = int(a)
b = int(b)
try:
    print(a/b)
except ZeroDivisionError:
    print("b can not be zero.")
    # https://docs.python.org/ja/3/library/exceptions.html #ZeroDivisionError 組み込み例外処理
type a number: 10
type a number: 0
b can not be zero.
# 異なる型を受け取ってしまう場合のコード処理対策
a = input('type a number: ')
b = input('type a number: ')
a = int(a)
b = int(b)
try:
    print(a/b)
except ZeroDivisionError:
    print("b can not be zero.")
    # https://docs.python.org/ja/3/library/exceptions.html #ZeroDivisionError 組み込み例外処理
type a number: 10
type a number: akihiro



---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-3-a9d3cc335648> in <module>
      3 b = input('type a number: ')
      4 a = int(a)
----> 5 b = int(b)
      6 try:
      7     print(a/b)


ValueError: invalid literal for int() with base 10: 'akihiro'
# ZeroDivisionError, ValueErrorの両方のエラーが生じたときを想定してコードで解決
try:
    a = input('type a number: ')
    b = input('type a number: ')
    a = int(a)
    b = int(b)
    print(a/b)
except (ZeroDivisionError, ValueError):
    print("invalid input!")
type a number: 10
type a number: akihiro
invalid input!
try:
    a = input('type a number: ')
    b = input('type a number: ')
    a = int(a)
    b = int(b)
    print(a/b)
except (ZeroDivisionError, ValueError):
    print("invalid input!")
type a number: 10
type a number: 0
invalid input!
try:
    10/0
    c = 'i will never get defined'
except ZeroDivisionError:
    print(c)
---------------------------------------------------------------------------

ZeroDivisionError                         Traceback (most recent call last)

<ipython-input-15-3b707af06240> in <module>
      1 try:
----> 2     10/0
      3     c = 'i will never get defined'


ZeroDivisionError: division by zero


During handling of the above exception, another exception occurred:


NameError                                 Traceback (most recent call last)

<ipython-input-15-3b707af06240> in <module>
      3     c = 'i will never get defined'
      4 except ZeroDivisionError:
----> 5     print(c)


NameError: name 'c' is not defined

コンテナ

  • リスト
  • タプル
  • 辞書

メソッド

  • 特定のデータ型に密接に関連付けられている関数
  • 通常の関数と同じような性質を持ち、引数を渡すことができ、また、コードを実行して値を返す
  • 違う点は、オブジェクトにつけて呼び出すという点
"hello".upper()
'HELLO'
"HELLO".lower()
'hello'

リスト

  • 好きな順番でオブジェクトを保存しておけるコンテナ
  • どんな種類のオブジェクトでも追加できる
fruit = ["apple", "orange", "grape"]
fruit.append('banana') #appned(追加)メソッド
fruit[3]
# for s in fruit:
#     print(s)
fruit
# (回りくどい説明をすれば)appendメソッドを使って渡したオブジェクトがリストの末尾に追加された
['apple', 'orange', 'grape', 'banana']
rnd = []
rnd.append('banana')
rnd.append(100)
rnd.append(True)
rnd
['banana', 100, True]
#上書き、削除(末尾)
colors = ['red', 'blue', 'yelllow', 'pink']
colors[1] = 'light-blue'
colors
colors.pop()
colors
['red', 'light-blue', 'yelllow']
colors[1:3]
['light-blue', 'yelllow']
colors[ : ]
['red', 'light-blue', 'yelllow']
# 要素が含まれているかを調べる
'green' in colors
False
'green' not in colors
True
# リストのサイズ
print(colors)
len(colors)
['red', 'light-blue', 'yelllow']





3
# 小さなプログラムを作ってみる
items = ['red', 'blue', 'yelllow', 'pink']
guess = input('色を指定してください:')
if guess in items:
    print('あたり!')
else:
    print('はずれ')
色を指定してください:red
あたり!

タプル 

  • 好きな順番でオブジェクトを保存しておけるコンテナ
  • リストと異なり、タプルはイミュータブル(変更不可能)な性質をもつため、一度タプルを作ると、要素の削除、追加ができない
    • 変わってほしくない値を格納しておくことに強み
    • 地図上の座標など
    • 逆にリストに入れるとプログラムの中で編集される危険性がある
  • タプルは()を使用する
#作り方は2つ
my_tuple = tuple()
my_tuple
()
my_tuple2 = ()
my_tuple2
#空の状態のtupleにオブジェクトを追加するときは後者の構文を使用する
()
rndm = ("akihiro", 1995, True)
rndm
('akihiro', 1995, True)
#タプルの要素が一つのときは、末端に,をつけること

('self_taught', )#これはタプル
('self_taught',)
(10)#これは数値計算に使うカッコ
10
#タプルに値を追加してみる
my_introduce = ('akihiro', 1995, 'self_taught of python', 'osaka')
my_introduce[1] = 1996
----------------------------------------------------------------------

TypeError                            Traceback (most recent call last)

<ipython-input-46-f4c942b21623> in <module>
      1 #タプルに値を追加してみる
      2 my_introduce = ('akihiro', 1995, 'self_taught of python', 'osaka')
----> 3 my_introduce[1] = 1996


TypeError: 'tuple' object does not support item assignment
#あとはリストと同じ操作ができる
my_introduce[1]
1995
1995 in my_introduce
True
1995 not in my_introduce
False

辞書

  • 2つのオブジェクトを関連付けて保持できるコンテナ
  • 片方のオブジェクトを格納時や取得時のキーに使い、もう片方のオブジェクトをバリューとしてキーにマッピングして(関連付けて)保持する
  • キーバリューペアと呼ぶ
  • 辞書からは、キーを使ってバリューを取り出すことができる。その逆はできない。
  • 辞書はミュータブル(変更可能)
  • リストやタプルと違い、辞書に格納するオブジェクトの順番は指定できない
  • 辞書は、{}を使用して作る
#作り方は二通り

my_dict = dict()
my_dict
{}
my_dict2 = {}
my_dict2
{}
fruits = {'apple':'red', 'banana':'yellow'} #要素が一つでも末尾に,は不要
fruits
{'apple': 'red', 'banana': 'yellow'}
fruits['apple']
'red'
fruits['banana']
'yellow'
fruits['red'] #これはできない
----------------------------------------------------------------------

KeyError                             Traceback (most recent call last)

<ipython-input-62-c5c2297ac982> in <module>
----> 1 fruits['red'] #これはできない


KeyError: 'red'
my_family = {'Akihiro':1995, 'my_cat':2012} # バリューはどんなオブジェクトも格納可能、ここでは整数オブジェクトを格納している
  • 辞書のバリューには、どんなオブジェクトも格納可能
  • 辞書のキーは、イミュータブルなため、文字列かタプルなら辞書のキーに使用できるが、リストや辞書は辞書のキーに使用できない
'Akihiro' in my_family #キーを使って検索は可能
True
#削除するとき
del my_family['Akihiro']
my_family
{'my_cat': 2012}

辞書を使って小さなプログラムを書いてみる

テーマ:カラオケの音楽プログラム

Fukuyama_songs = {
    '1': 'sakurazaka',
    '2': 'hello',
    '3': 'gunjou',
    '4': 'niji'
}
num = input('please type a number')
if num in Fukuyama_songs:
    print(Fukuyama_songs[num])
else:
    print('can not find the music')
please type a number2
hello

コンテナ中のコンテナ

#リストの中のリストを作ってみる(リストにタプルを持たせたりもできる)

lists = []

rap = ['Tom', 'Mary', 'Jane']
rock = ['Ken', 'Max', 'Kate']
djs = ['Ted', 'Jane', 'Rick']

lists.append(rap)
lists.append(rock)
lists.append(djs)

print(lists)

#要素へのアクセス方法と追加方法
rap = lists[0]
rap.append('Hiroshi')
print(rap)

lists
[['Tom', 'Mary', 'Jane'], ['Ken', 'Max', 'Kate'], ['Ted', 'Jane', 'Rick']]
['Tom', 'Mary', 'Jane', 'Hiroshi']





[['Tom', 'Mary', 'Jane', 'Hiroshi'],
 ['Ken', 'Max', 'Kate'],
 ['Ted', 'Jane', 'Rick']]

for文

name = 'Ted'
for c in name:
    print(c)
# name: 文字列 → イテラブル(繰り返し可能):文字列やリストやタプルのように、繰り返し処理で要素を取り出せるオブジェクト
# for val in iterable:
#     code
T
e
d
#文字列
name = 'akihiro'
for c in name:
    print(c)
a
k
i
h
i
r
o
#リスト
name = ['Sebastian', 'Vettel'] # ※好きなF1ドライバー
for s in name:
    print(s)
Sebastian
Vettel
#タプル
name = ('Sebastian', 'Vettel')
for s in name:
    print(s)
Sebastian
Vettel
#辞書
name = {'first_name':'Sebastian', 'last_name':'Vettel'}
for s in name:
    print(s)
#     辞書型ではキーが要素として出力された
first_name
last_name

country = ['japan', 'america', 'korea']
i = 0
for name in country:
    new = country[i]
    new = new.upper() #new上書き
    country[i] = new #coutry要素上書き
    i += 1

print(country)

#大文字へ変換させるために、newという一時的な変数を使って、coutrynの各要素を上書きする
['JAPAN', 'AMERICA', 'KOREA']
#保留 → 質問

country = ['japan', 'america', 'korea']
# initial = ['J', 'A', 'K']
country[0][0] = 'J'
# country[0][0] = initial[0]
country

# i = 0
# for name in country:
#     country[i][0].upper()
#     i += 1
# print(country)


# i = 0
# initial = []
# for name in country:
#     initial.append(country[i][0]) 
#     new = initial[i]
#     new = new.upper() #new上書き
#     initial[i] = new
#     print(initial[i])
#     print(country[i][0])
# #     country[i][0] = initial[i]
#     i += 1

# print(country)

#大文字へ変換させるために、newという一時的な変数を使って、countryの各要素を上書きする
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-145-5f44837de411> in <module>
      3 country = ['japan', 'america', 'korea']
      4 # initial = ['J', 'A', 'K']
----> 5 country[0][0] = 'J'
      6 # country[0][0] = initial[0]
      7 country


TypeError: 'str' object does not support item assignment
country = ['japan', 'america', 'korea']
i = 0

for name in country:

    tmp = country[i]
    tmp = tmp.upper() #new上書き
    country[i] = tmp #coutry要素上書き
    i += 1


print(country)

#大文字へ変換させるために、newという一時的な変数を使って、coutrynの各要素を上書きする
['JAPAN', 'AMERICA', 'KOREA']
country = ['japan', 'america', 'korea']
i = 0

for i, tmp in enumerate(country):

    tmp = country[i]
    tmp = tmp.upper() #new上書き
    country[i] = tmp #coutry要素上書き
#     i += 1
    print(i, country)# enumerate()でインデックスを取得できる


print(country)

#大文字へ変換させるために、newという一時的な変数を使って、coutrynの各要素を上書きする
0 ['JAPAN', 'america', 'korea']
1 ['JAPAN', 'AMERICA', 'korea']
2 ['JAPAN', 'AMERICA', 'KOREA']
['JAPAN', 'AMERICA', 'KOREA']
pref_east = ['kanagawa', 'saitama', 'tokyo' , 'chiba']
pref_west = ['osaka', 'kyoto', 'hyogo', 'hiroshima']
pref = []


for tmp in pref_east:
    pref.append(tmp.upper())


for tmp in pref_west:
    pref.append(tmp.upper())


print(pref)

['KANAGAWA', 'SAITAMA', 'TOKYO', 'CHIBA', 'OSAKA', 'KYOTO', 'HYOGO', 'HIROSHIMA']

while文

x = 10
while x > 0:
    if x == 1:
        print('{}'.format(x), end='')
    else:
        print('{},'.format(x), end='')

#     print('{}'.format(x))
    x -= 1

10,9,8,7,6,5,4,3,2,1

モジュール

組込みモジュール

  • python本体に含まれていて重要な機能を提供するモジュール
  • import [モジュール名]
import math
math.pow(math.sqrt(2), 2)
math.pow(2,3)
8.0
import random 

count = 0
for i in range(100):

    if random.random() < 0.1:
#         print('あたり!')
        count += 1
    else:
        pass
#         print('はずれ!')

print('あたりの回数は,{}回/100回'.format(count))
あたりの回数は,11回/100回

オブジェクト指向プログラミング

  • 用語理解
    • 数値123というオブジェクトは、intクラスのインスタンス
    • 'abc'という文字列オブジェクトは、strクラスのインスタンス
    • pythonではすべてのオブジェクトは何らかのインスタンスである
    • 故に、数値を単純に並べれば、intクラスのインスタンスとなり、' 'で囲えばstrクラスのインスタンスとなる
#クラスを作成してみる

class Orange:
    def __init__(self, w, c):
        #インスタンス変数は__init__メソッドの中で定義する
        #__init__メソッドは特殊メソッドであり、pythonがオブジェクトの作成など特別な目的で使用するメソッド
        #以下のように書くと,weightとcolorというインスタンス変数が作成される
        #クラス内のどのメソッドでもこれらのインスタンス変数を通常の変数のように扱うことができる
        self.weight = w
        self.color = c

        print('created!')


#オブジェクトを作成
#クラス名(引数), 引数部分には__init__メソッドが受け取れる引数を渡す、selfを明示的に渡す必要なし、pythonが自動的に渡してくれる
orl = Orange(10, 'dark')
#作成に成功したら、created!が表示される
print(orl)
#このように新しいオブジェクトを作ることを、クラスのインスタンス化という

#オブジェクト作成後にインスタンス変数の値へのアクセス方法↓
print(orl.weight)
print(orl.color)

#インスタンス変数の値の変更
orl.weight = 100
print(orl.weight)


#オブジェクトを更に作成
orl2 = Orange(50, 'light')
orl3 = Orange(70, 'dark')




#以上のように、オブジェクトを作成するまでに、__init__メソッドを作成して、インスタンス変数を定義し、最後にオブジェクトを作成した

created!
<__main__.Orange object at 0x7fee9ae1f1c0>
10
dark
100
created!
created!
#次に上のコードを一部利用して、メソッドの追加とインスタンス変数の追加をしてみる


class Orange:
    def __init__(self, w, c):
        self.weight = w
        self.color = c
        self.mold = 0 #クラス内で使用する変数を定義する、クラス内のローカル変数
        print('created!')

    def rot(self, days, temp):
        self.mold = days * temp

orange1 = Orange(200, 'dark')
print(orange1.mold)
orange1.rot(10, 38)
print(orange1.mold)


created!
0
380
#もう少し練習

class Rectangle:
    def __init__(self, w, l):
        self.width = w
        self.len = l

    def area(self):
        return self.width * self.len

    def change_size(self, w, l):
        self.width = w #インスタンス変数の値を上書き
        self.len = l

rectangle = Rectangle(10, 30)
print(rectangle.area()) #クラス内メソッドの使用方法
rectangle.change_size(5, 10)
print(rectangle.area())
300
50

データ分析のためのpython文法基礎

  • Numpy : 数値計算、行列演算
  • Pandas:データの操作、演算
  • Matplotlib : グラフの描画

参考資料:
【最短で理解する】データ分析のためのPython基礎

numpy

import numpy as np
array1 = np.array([1,2,3,4,5])
array1
array([1, 2, 3, 4, 5])
array1.shape
(5,)
array1[2]
3
array1[1:5]
array([2, 3, 4, 5])
array1[ : ]
array([1, 2, 3, 4, 5])
array2 = np.array([[1,2,3], [4,5,6]])
array2
array([[1, 2, 3],
       [4, 5, 6]])
array2[1, 1]
5
array2[1, 2]
6
array2.shape
(2, 3)
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
a
array([[1, 2],
       [3, 4]])
b
array([[5, 6],
       [7, 8]])
np.dot(a,b)#行列の積の計算
array([[19, 22],
       [43, 50]])

matplotlib

import matplotlib.pyplot as plt # Matplotlibパッケージのpyplotモジュールをインポート
# from matplotlib import pyplot as plt # こんなふうに書いてもいい   
import numpy as np
x = np.linspace(0, 10, 100) # 変数xにNumPyの関数linspaceを使用し0から10までの数字を100分割したものを代入
y = x
plt.plot(x, y) #xとyをプロット
plt.show() #グラフを表示

png



import matplotlib.pyplot as plt   
import numpy as np
import sys
print(sys.path)
import japanize_matplotlib
['/home/akihiro/anaconda3/envs/python_practice', '/home/akihiro/anaconda3/envs/python_practice/lib/python38.zip', '/home/akihiro/anaconda3/envs/python_practice/lib/python3.8', '/home/akihiro/anaconda3/envs/python_practice/lib/python3.8/lib-dynload', '', '/home/akihiro/.local/lib/python3.8/site-packages', '/home/akihiro/anaconda3/envs/python_practice/lib/python3.8/site-packages', '/home/akihiro/anaconda3/envs/python_practice/lib/python3.8/site-packages/IPython/extensions', '/home/akihiro/.ipython']
x = np.linspace(-5, 5, 10)
y = x**2

plt.plot(x, y, color = 'b')
plt.xlabel("y=x^2のグラフ")
plt.show()

png

import math

x = np.linspace(-np.pi, np.pi)
plt.plot(x, np.cos(x), color = 'b', ls = '-', label = 'cos')
plt.plot(x, np.sin(x), color = 'r', ls = '-', label = 'sin')
plt.plot(x, np.tan(x), color = 'y', ls = '-', label = 'tan')

plt.xlim(-math.pi, math.pi)
plt.ylim(-1, 1)


plt.legend()
plt.xlabel('x軸')
plt.ylabel('y軸')
plt.title('三角関数のグラフ')

# plt.plot(x, y) #xとyをプロット
3 plt.show() #グラフを表示

png

pandas

  • pandasのDataFrameでは列ごとに異なるデータ型であっても構わない
  • Numpy配列はすべて同じデータ型である必要がある
import pandas as pd
df1 = pd.DataFrame({
          '名前':['sato','ito','kato','endo','naito'],
          '学生番号':[1,2,3,4,5],
          '体重':[92,43,58,62,54],
          '身長':[178,172,155,174,168]
          })
df1
名前 学生番号 体重 身長
0 sato 1 92 178
1 ito 2 43 172
2 kato 3 58 155
3 endo 4 62 174
4 naito 5 54 168
df2 = pd.DataFrame({
          '学生番号':[1,2,3,5,6,9],
          '数学':[50,60,70,80,90,100],
          '英語':[95,85,80,75,70,65],
          '理科':[40,55,60,65,50,75],
          'クラス':['A組','B組','A組','C組','B組','C組']
          })
df2
学生番号 数学 英語 理科 クラス
0 1 50 95 40 A組
1 2 60 85 55 B組
2 3 70 80 60 A組
3 5 80 75 65 C組
4 6 90 70 50 B組
5 9 100 65 75 C組

データの抽出

df2['数学']
0     50
1     60
2     70
3     80
4     90
5    100
Name: 数学, dtype: int64
df2['クラス']
0    A組
1    B組
2    A組
3    C組
4    B組
5    C組
Name: クラス, dtype: object
df2[['数学', 'クラス']] #複数列を抽出する際は[ ]を二重にして呼び出します。 
数学 クラス
0 50 A組
1 60 B組
2 70 A組
3 80 C組
4 90 B組
5 100 C組
df2[['学生番号', '数学']] 
学生番号 数学
0 1 50
1 2 60
2 3 70
3 5 80
4 6 90
5 9 100

df2[ '英語'].sort_values()
5    65
4    70
3    75
2    80
1    85
0    95
Name: 英語, dtype: int64
df1['身長'].sort_values() #一部をソート
2    155
4    168
1    172
3    174
0    178
Name: 身長, dtype: int64
df1.sort_values(by=['身長'], ascending = False) #一部をソートして全体を表示, False:降順
名前 学生番号 体重 身長
0 sato 1 92 178
3 endo 4 62 174
1 ito 2 43 172
4 naito 5 54 168
2 kato 3 58 155

データの結合

  • 内部結合
  • 外部結合
  • 左外部結合
  • 右外部結合

内部結合→共通する部分のみを結合

data_inner = pd.merge(df1, df2, on='学生番号', how='inner')# on='学生番号':共通部分を指定
data_inner
名前 学生番号 体重 身長 数学 英語 理科 クラス
0 sato 1 92 178 50 95 40 A組
1 ito 2 43 172 60 85 55 B組
2 kato 3 58 155 70 80 60 A組
3 naito 5 54 168 80 75 65 C組

外部結合→全部を無理やり結合

data_outer = pd.merge(df1, df2, how = 'outer')
data_outer
名前 学生番号 体重 身長 数学 英語 理科 クラス
0 sato 1 92.0 178.0 50.0 95.0 40.0 A組
1 ito 2 43.0 172.0 60.0 85.0 55.0 B組
2 kato 3 58.0 155.0 70.0 80.0 60.0 A組
3 endo 4 62.0 174.0 NaN NaN NaN NaN
4 naito 5 54.0 168.0 80.0 75.0 65.0 C組
5 NaN 6 NaN NaN 90.0 70.0 50.0 B組
6 NaN 9 NaN NaN 100.0 65.0 75.0 C組

左外部結合

data_left = pd.merge(df1, df2, how='left')
data_left
# left joinでは結合先(df1)に存在するものは残ります。inner joinの時とは違い、欠損値を含めてendoさんのデータが表示されています。left joinでは結合先(df1)に存在するものは残ります。
#inner joinの時とは違い、欠損値を含めてendoさんのデータが表示されています。
名前 学生番号 体重 身長 数学 英語 理科 クラス
0 sato 1 92 178 50.0 95.0 40.0 A組
1 ito 2 43 172 60.0 85.0 55.0 B組
2 kato 3 58 155 70.0 80.0 60.0 A組
3 endo 4 62 174 NaN NaN NaN NaN
4 naito 5 54 168 80.0 75.0 65.0 C組

カラムの追加

df2['理系科目'] = df2['数学'] + df2['理科']
df2
学生番号 数学 英語 理科 クラス 理系科目
0 1 50 95 40 A組 90
1 2 60 85 55 B組 115
2 3 70 80 60 A組 130
3 5 80 75 65 C組 145
4 6 90 70 50 B組 140
5 9 100 65 75 C組 175
df2.drop('合計得点' ,axis = 1)
学生番号 数学 英語 理科 クラス 理系科目 Sum
0 1 50 95 40 A組 90 185
1 2 60 85 55 B組 115 200
2 3 70 80 60 A組 130 210
3 5 80 75 65 C組 145 220
4 6 90 70 50 B組 140 210
5 9 100 65 75 C組 175 240
df2.drop('合計得点' ,axis = 1)
df2.sort_values(by=['Sum'], ascending = False) #一部をソートして全体を表示, False:降順
学生番号 数学 英語 理科 クラス 理系科目 合計得点 Sum
5 9 100 65 75 C組 175 240 240
3 5 80 75 65 C組 145 220 220
2 3 70 80 60 A組 130 210 210
4 6 90 70 50 B組 140 210 210
1 2 60 85 55 B組 115 200 200
0 1 50 95 40 A組 90 185 185
display(df2[df2.Sum > 210]) # 条件をつけてデータを抽出することもできる
学生番号 数学 英語 理科 クラス 理系科目 合計得点 Sum
3 5 80 75 65 C組 145 220 220
5 9 100 65 75 C組 175 240 240






作って学ぶPython入門 #002 (youtubeで学習)

参考資料:

作って学ぶPython入門 #002 「間違い探しクイズをつくろう」Pythonのできることの例: 文字列置換、ファイル書き込み。

formatメソッドを使った文字列の書式設定

Pythonでランダムな小数・整数を生成するrandom, randrange, randintなど

import random 


#main関数を定義
def main():
    # rnd = random.randint(50, 100)
    # rnd = random.random()
    count = 0
    positions = []
    problem = ""
    A = 'ぬ'
    B = 'め'

   #ポイント:行列計算で二重ループはよく使う
    for x in range(1, 11):
        for y in range(1, 11):

            if y % 10 == 0:
                print(A)
                #ポイント:文字列の足し算
                problem += A + '\n'
            else: 
                #ポイント:ランダム関数
                if random.random() < 0.1:
                    print(B, end = '')
                    count += 1
                    problem += B

                    #ポイント:リストに要素を追加
                    p = [x, y]
                    positions.append(p)
                else:
                    print(A, end = '')
                    problem += A


    # print(count)
    # print(positions)
    print('========================')
    print(problem)


    #ポイント:formatメソッド、print関数とともに使用する。print関数は簡単に値を確認する(デバッグ)ために使うことが多い
    print("「め」の数は{}個\n".format(count))

    for a in positions:
        print("「め」は{}行, {}列目\n".format(a[0], a[1]))


    #ポイント:ファイルへ結果を書き込み ,withを使用すると自動でcloseしてくれる
    with open('result.txt', 'w') as f:
        f.write('間違い探しクイズ!\n')
        f.write(problem)
        f.write('答え:\n')
        f.write("「め」の数は{}個\n".format(count))

        for a in positions:
            f.write("「め」は{}行, {}列目\n".format(a[0], a[1]))






if __name__ == '__main__':
    main()
ぬぬぬぬぬぬぬぬぬぬ
ぬぬぬぬぬめぬぬぬぬ
ぬめぬぬぬぬぬぬぬぬ
ぬぬぬぬぬぬぬぬぬぬ
ぬぬぬぬぬぬめぬぬぬ
ぬぬぬぬぬぬぬぬめぬ
ぬぬぬぬぬぬぬぬぬぬ
ぬぬめぬぬぬぬぬぬぬ
ぬぬぬめぬぬめぬぬぬ
ぬぬぬぬめぬぬぬぬぬ
========================
ぬぬぬぬぬぬぬぬぬぬ
ぬぬぬぬぬめぬぬぬぬ
ぬめぬぬぬぬぬぬぬぬ
ぬぬぬぬぬぬぬぬぬぬ
ぬぬぬぬぬぬめぬぬぬ
ぬぬぬぬぬぬぬぬめぬ
ぬぬぬぬぬぬぬぬぬぬ
ぬぬめぬぬぬぬぬぬぬ
ぬぬぬめぬぬめぬぬぬ
ぬぬぬぬめぬぬぬぬぬ

「め」の数は8個

「め」は2行, 6列目

「め」は3行, 2列目

「め」は5行, 7列目

「め」は6行, 9列目

「め」は8行, 3列目

「め」は9行, 4列目

「め」は9行, 7列目

「め」は10行, 5列目
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0