Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
- 'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'],
- 'wins': [11, 8, 10, 15, 11, 6, 10, 4],
- 'losses': [5, 8, 6, 1, 5, 10, 6, 12]}
- football = pd.DataFrame(data)
- football
- # year team wins losses
- # 0 2010 Bears 11 5
- # 1 2011 Bears 8 8
- # 2 2012 Bears 10 6
- # 3 2011 Packers 15 1
- # 4 2012 Packers 11 5
- # 5 2010 Lions 6 10
- # 6 2011 Lions 10 6
- # 7 2012 Lions 4 12
- # การเลือก 1 Column จะได้ข้อมูลออกมาเป็น Series
- football['year']
- football.year # shorthand for football['year']
- # 0 2010
- # 1 2011
- # 2 2012
- # 3 2011
- # 4 2012
- # 5 2010
- # 6 2011
- # 7 2012
- # Name: year, dtype: int64
- # การเลือกหลาย Columns จะได้ข้อมูลออกมาเป็น DataFrame
- football[['year', 'wins', 'losses']]
- # year wins losses
- # 0 2010 11 5
- # 1 2011 8 8
- # 2 2012 10 6
- # 3 2011 15 1
- # 4 2012 11 5
- # 5 2010 6 10
- # 6 2011 10 6
- # 7 2012 4 12
- # การเลือก Row สามารถทำได้หลายวิธี
- # ระบุ Index โดยใช้ฟังก์ชั่น iloc หรือ loc
- football.iloc[[0]]
- football.loc[[0]]
- # year team wins losses
- # 0 2010 Bears 11 5
- # ใช้การทำ Slicing
- football[3:5]
- # year team wins losses
- # 3 2011 Packers 15 1
- # 4 2012 Packers 11 5
- # ใช้การทำ Boolean Indexing
- football[football.wins > 10]
- # year team wins losses
- # 0 2010 Bears 11 5
- # 3 2011 Packers 15 1
- # 4 2012 Packers 11 5
- football[(football.wins > 10) & (football.team == "Packers")]
- # year team wins losses
- # 3 2011 Packers 15 1
- # 4 2012 Packers 11 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement