IT's 2 EG

유용한 라이브러리 본문

Data Scientist/Python 라이브러리 기초 사용법

유용한 라이브러리

엠씨비기 2021. 7. 13. 22:56
CH 01. 파이썬 기초 - 기타 라이브러리 사용

math 라이브러리를 활용한 로그 연산

In [1]:
import math

print(math.log(5)) # 자연로그
print(math.log2(32)) # 밑이 2인 로그
print(math.log10(100)) # 밑이 10인 로그
1.6094379124341003
5.0
2.0

지수연산

In [2]:
print(2**5) # 2의 5제곱
print(2**0.5) # 루트 2
print(math.exp(3)) #e의 3제곱
32
1.4142135623730951
20.085536923187668

join() 함수

In [3]:
# ★★ formula 정의시 사용
"y ~ " + " + ".join(["x1", "x2", "x3"])
Out[3]:
'y ~ x1 + x2 + x3'

rank()함수 : Series에서 순위를 구하는 함수

In [4]:
import pandas as pd

class_score = pd.read_csv("dataset/class_scores.csv")
In [5]:
class_score_g1 = class_score.loc[class_score["grade"] == 1, ["Math"]]
In [6]:
class_score_g1["rank"] = class_score_g1.rank(ascending=True) # 오름차순으로 순위를 작성
class_score_g1.head()
Out[6]:
Math rank
0 55 72.0
1 29 14.0
2 28 10.0
3 45 47.0
4 28 10.0
In [7]:
class_score_g1["rank"] = class_score_g1.rank(ascending=False) # 내림차순으로 순위를 작성
class_score_g1.head()
Out[7]:
Math rank
0 55 129.0
1 29 187.0
2 28 191.0
3 45 154.0
4 28 191.0

method 옵션을 통한 동점치 순위 지정방식 변경

  • method = "average" : 동점 관측치 간의 그룹 내 평균 순위 부여
  • method = "min" : 동점 관측치 그룹 내 최소 순위 부여
  • method = "max" : 동점 관측치 그룹 내 최대 순위 부여
  • method = "first" : 동점 관측치 중에서 데이터 상에서 먼저 나타나는 관측치부터 순위 부여
  • method = "dense" : 'min'과 같은 방법으로 순위 부여 하나, 'min'과 다르게 그룹간 순위가 1씩 증가
In [8]:
class_score_g1["rank"] = class_score_g1.rank(ascending=False, method = "average")
class_score_g1.head()
Out[8]:
Math rank
0 55 129.0
1 29 187.0
2 28 191.0
3 45 154.0
4 28 191.0
In [9]:
class_score_g1["rank"] = class_score_g1.rank(ascending=False, method = "min")
class_score_g1.head()
Out[9]:
Math rank
0 55 127.0
1 29 185.0
2 28 190.0
3 45 153.0
4 28 190.0
In [10]:
class_score_g1["rank"] = class_score_g1.rank(ascending=False, method = "max")
class_score_g1.head()
Out[10]:
Math rank
0 55 131.0
1 29 189.0
2 28 192.0
3 45 155.0
4 28 192.0
In [11]:
class_score_g1["rank"] = class_score_g1.rank(ascending=False, method = "first")
class_score_g1.head()
Out[11]:
Math rank
0 55 127.0
1 29 185.0
2 28 190.0
3 45 153.0
4 28 191.0
In [12]:
class_score_g1["rank"] = class_score_g1.rank(ascending=False, method = "dense")
class_score_g1.head()
Out[12]:
Math rank
0 55 41.0
1 29 62.0
2 28 63.0
3 45 50.0
4 28 63.0

'Data Scientist > Python 라이브러리 기초 사용법' 카테고리의 다른 글

numpy 사용법 기초  (0) 2021.04.30
Comments