Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- jsonlines
- wordembedding
- 텐서플로자격증
- json
- BloombergMarketConcepts
- sshtunnel
- VScodeNotResponding
- iterrows
- TensorflowDeveloperCertificate
- session-basedRecommendation
- MySQL
- 지도시각화
- ExplicitData
- Convert
- str.replace
- numpy.bool
- Visualization
- vscode
- Python
- Colab
- MatrixFactorization
- pandas
- DIF_SR
- implicitData
- LatentFactorModel
- Cast
- jsonl
- gluonnlp
- decimal error
- github2FA
Archives
- Today
- Total
garret
[Python] pandas.DataFrame.sample() 함수 본문
정의
Return a random sample of items from an axis of object
즉, 데이터프레임에서 랜덤하게 샘플링하고 싶을 때 사용하는 함수
DataFrame.sample(n=None, frac=None, replace=False, weights=None,
random_state=None, axis=None, ignore_index=False)
예시
먼저 DataFrame 준비하기.
>>> import pandas as pd
>>> df = pd.DataFrame({'size': [2, 4, 8, 1],
... 'order_counts': [0, 5, 8, 2],
... 'num_counts': [10, 2, 1, 8]},
... index=['plum', 'peach', 'watermelon', 'blueberry'])
>>> df
size order_counts num_counts
plum 2 0 10
peach 4 5 2
watermelon 8 8 1
blueberry 1 2 8
n : 랜덤하게 뽑을 데이터의 개수
>>> df['size'].sample(n=3, random_state=1)
blueberry 1
watermelon 8
plum 2
Name: size, dtype: int64
frac : 랜덤하게 뽑을 데이터의 비율.
>>> df.sample(frac=0.5, replace=True, random_state=1)
size order_counts num_counts
peach 4 5 2
blueberry 1 2 8
df.sample(frac = 0.7) # 70% 데이터 랜덤하게 return
df.sample(frac = 1) # 100%의 데이터 랜덤하게 return. 즉, shuffle의 기능 수행
df.sample(frac = 2) # 200%의 데이터 랜덤하게 return
Reference
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sample.html
'Programming > Python' 카테고리의 다른 글
[Python, 환경설정] conda에서 python 가상환경 생성 (0) | 2023.06.14 |
---|---|
[Python] pandas.DataFrame.select_dtypes 함수 (0) | 2023.06.13 |
[Python] slice()함수 vs. islice() 함수 (0) | 2023.06.02 |
[Python] split() 함수 vs. join() 함수 (0) | 2023.06.01 |
[Python] sys.stdout.write()와 print() 차이 (0) | 2023.05.23 |