Question
werid = pd.DataFrame({1:["topdog","botdog"], "1":["topcat","botcat"]})
werid
Try to predict the output of the following:
- weird[1]
- werid["1"]
- werid[1:]
Name --> [ ] --> Series (Single Column Selection)
List --> [ ] --> DataFrame (Multiple Column Selection)
Numeric Slice -- > [ ] --> DataFrame (Multiple Raw Selection)
Answer:
weird[1]
weird["1"], werid[['1']], werid['1']
weird[1: ]
# boolean example
iswin = elections['Result'] == 'win'
elections[iswin]
# This show only 'win' results
# do not use 'and' and 'or' in python
# TypeError!
elections[(elections['Result'] == 'win')
& (elections['%'] < 50)]
# series
# elections['Candidate']
elections.loc[:, 'Candidate']
# real world syntax
# return row 0, 2, 3 with feature: Candidate, Party, Year
elections.loc[[0, 2, 3],
['Candidate', 'Party', 'Year']]
# try first row with every columns
# series
elections.loc[1, :]
# dataframe
elections.loc[[1], :]
iloc
# every row with column: 0, 1, 2
elections.iloc[:, [0, 1, 2]]
# row: 0,1,2 column: 0, 1, 2, 3
elections.iloc[0:3, 0:4]
# In contrast to loc, iloc selection by position
The sort_values Method
elections.sort_values('%', ascending=False)
# default: ascending==Ture
mottos['Language'].sort_values().head(5)
The value_counts Method
# creates a new Series showing the counts of every value
elections['Party'].value_counts()
Question
# Filter rows for only 2017
# Sort descending by count
ca2017 = ca[ca['Year'] == 2017]
ca2017.sort_values('Count', ascending=False)
'Computer Science 🌋 > Machine Learning🐼' 카테고리의 다른 글
Data Cleaning Review (0) | 2023.05.28 |
---|---|
Pandas part 2 Review (0) | 2023.05.27 |
Life Cycle and Design Review (0) | 2023.05.26 |
Feature Engineering (0) | 2023.05.25 |
Canonicalization (0) | 2023.05.24 |