본문 바로가기

분석/파이썬 Python7

Python : 문자열안의 특정 부분을 변수치환 python 에서 문자열의 특정 부분을 치환하는 몇 가지 방법을 적어려고 한다. 1. 변수 포맷을 이용하는 방법 변수포맷은 아래와 같다 %s : 문자열 %c : 문자 %d : 정수 %f : 실수 아래와 같은 방법으로 string 안에 %s 라는 부분을 특정 변수로 치환할 수 있다. 2. format 함수 사용 이 경우, 변수의 타입을 명시하지 않아도 된다. 아래와 같이 사용할 수 있으며, {0}, {1} 과 같은 부분이 format 안의 인자와 차례로 매칭 치환된다. 3. f문자열 포맷 사용 문자열 앞에 f 를 붙이면 중괄호와 변수이름으로 치환을 할 수 있다. 아래의 예를 참고하자. 2024. 4. 18.
[Python] 순열검정 (비모수) 두 집단의 평균을 비교하는 방법에는 크게는 모수적 방법과 비모수적 방법이 있다. 여기서 모수적 방법이란 모집단이 정규분포를 한다는 가정하에, 평균과 분산같은 통계량을 이용하여 계산되는 방식이며 비모수적 방법이란 모집단의 분포와 관계없이 계산하는 방식을 말한다. 아래에 python sample code 를 소개하고자 한다. Colab에서 실행된 결과는 아래의 링크에서도 확인할 수 있다. https://colab.research.google.com/drive/1GOBBNP13if2PotGpUXgwiqcVQR8JjbmZ#scrollTo=uhPCOt8ii7YJ # -*- coding: utf-8 -*- """Permutation Test.ipynb Automatically generated by Colabora.. 2024. 1. 27.
Python : 위도.경도로 TimeZone 구하기 Python에서 위도와 경도 값으로 TimeZone 구하기 먼저 'timezonefinder' 라는 패키지를 설치하여야 한다. pip install timezonefinder 사용법은 아래와 같이 간단하다. from timezonefinder import TimezoneFinder tf = TimezoneFinder() latitude, longitude = 52.5061, 13.358 tf.timezone_at(lng=longitude, lat=latitude) # returns 'Europe/Berlin' * 만일 아래와 같이 DataFrame에 위.경도의 값이 있다고 하면 from timezonefinder import TimezoneFinder my_func = TimezoneFinder().ti.. 2020. 3. 10.
python : pd.to_numeric() VS astype(np.float64) import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(10**5, 10**7, (5,3)), columns=list('abc'), dtype=np.int64) df a b c 0 2368596 282593 7649457 1 6486779 5348256 790672 2 8468404 4682970 2904873 3 2271514 2908642 9272301 4 7811256 3652968 6715015 df.dtypes a int64 b int64 c int64 dtype: object df['a'] = df['a'].astype(float) df.dtypes a float64 b int64 c int64 dtype: obje.. 2019. 11. 27.
Python : Seaborn Visualization import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns # 데이터셋 iris = sns.load_dataset('iris') titanic = sns.load_dataset('titanic') tips = sns.load_dataset('tips') flights = sns.load_dataset('flights') x = iris.petal_length.values sns.rugplot(x) sns.kdeplot(x) sns.distplot(x, rug=True, kde=True, bins=50) plt.hist(x, bins=50) (array([ 2., 2., 7., 13., 13., 11.. 2019. 11. 25.
Python : Pandas Visualization pandas의 plot은 내부적으로 matplotlib.pyplot을 이용한다. import numpy as np import pandas as pd import matplotlib.pyplot as plt df1 = pd.DataFrame(np.random.randn(100, 3), index=pd.date_range('1/1/2019', periods=100), columns=['A', 'B', 'C']).cumsum() df1 A B C 2019-01-01 -0.896370 -1.962732 1.584821 2019-01-02 -0.248402 -3.101740 0.370419 2019-01-03 0.622560 -3.979711 1.666569 2019-01-04 1.239019 -3.443114.. 2019. 11. 25.
Python : timedelta(months=3) 방법 Python에서 사용할 수 있는 시간의 차이에 관련된 모듈은 datetime.timedelta 가 있습니다. 아래와 같이 사용할 수 있습니다. import datetime as dt now = dt.datetime.now() delta = dt.timedelta(hours=3) diff = now - delta 이 모듈에서 사용할 수 있는 옵션은 days hours seconds weeks 등이 있지만, months, years를 사용할 수는 없습니다. 그 대안으로 사용할 수 있는 모듈이 relativedelta 라는 모듈입니다. 그리고 그 사용은 아래와 같습니다. from dateutil.relativedelta import relativedelta import datetime as dt now = d.. 2019. 11. 12.