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)
]
'Python' 카테고리의 다른 글
[Python] Pandas Course on Kaggle - 4 (0) | 2022.01.20 |
---|---|
[Python] Pandas Course on Kaggle - 3 (0) | 2022.01.19 |
[Python] Pandas Course on Kaggle - 1 (0) | 2022.01.15 |
[Python] finterstellar를 사용하여 Stochastic으로 주식 매매 시그널 만들기 (2) | 2021.12.14 |
[Python] finterstellar를 사용하여 Bollinger Band로 주식 매매 시그널 만들기 (0) | 2021.12.12 |
댓글