Python/Pandas
[Python] df 행 반환
ds-nada
2023. 8. 16. 17:47
Reference
Pandas In Action
1. df 행 반환
import pandas as pd
nba = pd.read_csv(
'./Data/nba.csv',
parse_dates = ['Birthday'] # 'Birthday'의 데이터 유형을 날짜/시간(datetime)으로 강제 변환
)
nba
|
Name |
Team |
Position |
Birthday |
Salary |
0 |
Shake Milton |
Philadelphia 76ers |
SG |
1996-09-26 |
1445697 |
1 |
Christian Wood |
Detroit Pistons |
PF |
1995-09-27 |
1645357 |
2 |
PJ Washington |
Charlotte Hornets |
PF |
1998-08-23 |
3831840 |
3 |
Derrick Rose |
Detroit Pistons |
PG |
1988-10-04 |
7317074 |
4 |
Marial Shayok |
Philadelphia 76ers |
G |
1995-07-26 |
79568 |
... |
... |
... |
... |
... |
... |
445 |
Austin Rivers |
Houston Rockets |
PG |
1992-08-01 |
2174310 |
446 |
Harry Giles |
Sacramento Kings |
PF |
1998-04-22 |
2578800 |
447 |
Robin Lopez |
Milwaukee Bucks |
C |
1988-04-01 |
4767000 |
448 |
Collin Sexton |
Cleveland Cavaliers |
PG |
1999-01-04 |
4764960 |
449 |
Ricky Rubio |
Phoenix Suns |
PG |
1990-10-21 |
16200000 |
450 rows × 5 columns
1-1. df 상위 행 :: df.head( )
df.head(
n = 5
)
nba.head(n = 5)
nba.head() # nba중 상위 5개의 행
|
Name |
Team |
Position |
Birthday |
Salary |
0 |
Shake Milton |
Philadelphia 76ers |
SG |
1996-09-26 |
1445697 |
1 |
Christian Wood |
Detroit Pistons |
PF |
1995-09-27 |
1645357 |
2 |
PJ Washington |
Charlotte Hornets |
PF |
1998-08-23 |
3831840 |
3 |
Derrick Rose |
Detroit Pistons |
PG |
1988-10-04 |
7317074 |
4 |
Marial Shayok |
Philadelphia 76ers |
G |
1995-07-26 |
79568 |
1-2. df 하위 행 :: df.tail( )
df.tail(
n = 5
)
nba.tail() # nba 중 하위 5개의 행
|
Name |
Team |
Position |
Birthday |
Salary |
445 |
Austin Rivers |
Houston Rockets |
PG |
1992-08-01 |
2174310 |
446 |
Harry Giles |
Sacramento Kings |
PF |
1998-04-22 |
2578800 |
447 |
Robin Lopez |
Milwaukee Bucks |
C |
1988-04-01 |
4767000 |
448 |
Collin Sexton |
Cleveland Cavaliers |
PG |
1999-01-04 |
4764960 |
449 |
Ricky Rubio |
Phoenix Suns |
PG |
1990-10-21 |
16200000 |
1-3. df 임의 행 :: df.sample()
df.sample(
n = None
random_state = None
)
- 객체의 축에서 임의의 항목 샘플을 반환
- Option
- n : 반환할 축의 항목 수
- random_state : 난수 생성기의 시드 값
nba.sample(3) # nba 데이터중 무작위 3개의 행
|
Name |
Team |
Position |
Birthday |
Salary |
420 |
John Henson |
Cleveland Cavaliers |
C |
1990-12-28 |
9732396 |
235 |
Al Horford |
Philadelphia 76ers |
C |
1986-06-03 |
28000000 |
35 |
Bradley Beal |
Washington Wizards |
SG |
1993-06-28 |
27093018 |