1 분 소요

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.col_name or df[‘col_name’]

nba.Name # nba의 'Name'열
0        Shake Milton
1      Christian Wood
2       PJ Washington
3        Derrick Rose
4       Marial Shayok
            ...      
445     Austin Rivers
446       Harry Giles
447       Robin Lopez
448     Collin Sexton
449       Ricky Rubio
Name: Name, Length: 450, dtype: object
nba['Name'] # nba의 'Name'열
0        Shake Milton
1      Christian Wood
2       PJ Washington
3        Derrick Rose
4       Marial Shayok
            ...      
445     Austin Rivers
446       Harry Giles
447       Robin Lopez
448     Collin Sexton
449       Ricky Rubio
Name: Name, Length: 450, dtype: object

1-2. df 다중 열 선택 :: df[[‘col_name1’, ‘col_name2’]]

nba[['Name','Team']] # nba의 'Name', 'Team' 열
  Name Team
0 Shake Milton Philadelphia 76ers
1 Christian Wood Detroit Pistons
2 PJ Washington Charlotte Hornets
3 Derrick Rose Detroit Pistons
4 Marial Shayok Philadelphia 76ers
445 Austin Rivers Houston Rockets
446 Harry Giles Sacramento Kings
447 Robin Lopez Milwaukee Bucks
448 Collin Sexton Cleveland Cavaliers
449 Ricky Rubio Phoenix Suns

450 rows × 2 columns

카테고리:

업데이트: