본문 바로가기
Python

[Python] Pandas Course on Kaggle - 2

by llHoYall 2022. 1. 17.

This is the solution of pandas course (Indexing, Selecting & Assigning) on Kaggle site.

1. Select the Column

desc = reviews["description"]
# or 
desc = reviews.description

2. Select the Column and Row

first_description = reviews["description"][0]
# or
first_description = reviews.description[0]
# or
first_description = reviews.description.loc[0]
# or
first_description = reviews.description.iloc[0]

3. Select the Row

first_row = reviews.loc[0]
# or
first_row = reviews.iloc[0]

4. Select the Rows

first_descriptions = reviews.description.head(10)
# or
first_descriptions = reviews.description.iloc[:10]
# or
first_descriptions = reviews.loc[:9, "description"]

5. Select the Specified Rows

sample_reviews = reviews.loc[[1, 2, 3, 5, 8]]
# or
sample_reviews = reviews.iloc[[1, 2, 3, 5, 8], :]

6. Select the Specified Columns and Rows

df = reviews.loc[
    [0, 1, 10, 100], 
    ["country", "province", "region_1", "region_2"]
]
# or
df = reviews.iloc[[0, 1, 10, 100], [0, 5, 6, 7]]

7. Select the Specified Columns and Rows 2

df = reviews.loc[:99, ["country", "variety"]]
# or
df = reviews.iloc[:100, [0, 11]]

8. Select the Rows Conditionally

italian_wines = reviews[reviews.country == "Italy"]
# or
italian_wines = reviews.loc[reviews.country == "Italy"]

9. Select the Rows as Multiple Condition

top_oceania_wines = reviews[
    (reviews.country.isin(['Australia', 'New Zealand'])) & 
    (reviews.points >= 95)
]
# or
top_oceania_wines = reviews.loc[
    (reviews.country.isin(['Australia', 'New Zealand'])) & 
    (reviews.points >= 95)
]

댓글