Post

[Python] df 열 반환

[Python] df 열 반환
Reference Pandas In Action

1. df 열 반환

1
2
3
4
5
6
7
import pandas as pd

nba = pd.read_csv(
    './Data/nba.csv',
    parse_dates = ['Birthday']  # 'Birthday'의 데이터 유형을 날짜/시간(datetime)으로 강제 변환
)
nba
 NameTeamPositionBirthdaySalary
0Shake MiltonPhiladelphia 76ersSG1996-09-261445697
1Christian WoodDetroit PistonsPF1995-09-271645357
2PJ WashingtonCharlotte HornetsPF1998-08-233831840
3Derrick RoseDetroit PistonsPG1988-10-047317074
4Marial ShayokPhiladelphia 76ersG1995-07-2679568
445Austin RiversHouston RocketsPG1992-08-012174310
446Harry GilesSacramento KingsPF1998-04-222578800
447Robin LopezMilwaukee BucksC1988-04-014767000
448Collin SextonCleveland CavaliersPG1999-01-044764960
449Ricky RubioPhoenix SunsPG1990-10-2116200000

450 rows × 5 columns

1-1. df 단일 열 선택 :: df.col_name or df[‘col_name’]

1
nba.Name # nba의 'Name'열
1
2
3
4
5
6
7
8
9
10
11
12
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
nba['Name'] # nba의 'Name'열
1
2
3
4
5
6
7
8
9
10
11
12
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’]]

1
nba[['Name','Team']] # nba의 'Name', 'Team' 열
 NameTeam
0Shake MiltonPhiladelphia 76ers
1Christian WoodDetroit Pistons
2PJ WashingtonCharlotte Hornets
3Derrick RoseDetroit Pistons
4Marial ShayokPhiladelphia 76ers
445Austin RiversHouston Rockets
446Harry GilesSacramento Kings
447Robin LopezMilwaukee Bucks
448Collin SextonCleveland Cavaliers
449Ricky RubioPhoenix Suns

450 rows × 2 columns

This post is licensed under CC BY 4.0 by the author.