import pandas as pd

reviews = pd.read_csv("../input/wine-reviews/winemag-data-130k-v2.csv", index_col=0)
pd.set_option("display.max_rows", 5)

from learntools.core import binder; binder.bind(globals())
from learntools.pandas.indexing_selecting_and_assigning import *
print("Setup complete.")

"""
Explanation: Introduction
In this set of exercises we will work with the Wine Reviews dataset. 
Run the following cell to load your data and some utility functions (including code to check your answers).
End of explanation
"""


reviews.head()

"""
Explanation: Look at an overview of your data by running the following line.
End of explanation
"""


# Your code here
desc = ____

# Check your answer
q1.check()

#%%RM_IF(PROD)%%
desc = reviews.description.values

q1.assert_check_failed()

#%%RM_IF(PROD)%%
desc = reviews.description

q1.assert_check_passed()

#%%RM_IF(PROD)%%
desc = reviews["description"]

q1.assert_check_passed()

"""
Explanation: Exercises
1.
Select the description column from reviews and assign the result to the variable desc.
End of explanation
"""


#_COMMENT_IF(PROD)_
q1.hint()
#_COMMENT_IF(PROD)_
q1.solution()

"""
Explanation: Follow-up question: what type of object is desc? If you're not sure, you can check by calling Python's type function: type(desc).
End of explanation
"""


first_description = ____

# Check your answer
q2.check()
first_description

#%%RM_IF(PROD)%%
first_description = desc[1]

q2.assert_check_failed()

#%%RM_IF(PROD)%%
first_description = desc.iloc[0]

q2.assert_check_passed()

#%%RM_IF(PROD)%%
first_description = desc.loc[0]

q2.assert_check_passed()

#%%RM_IF(PROD)%%
first_description = desc[0]

q2.assert_check_passed()

#_COMMENT_IF(PROD)_
q2.hint()
#_COMMENT_IF(PROD)_
q2.solution()

"""
Explanation: 2.
Select the first value from the description column of reviews, assigning it to variable first_description.
End of explanation
"""


first_row = ____

# Check your answer
q3.check()
first_row

#%%RM_IF(PROD)%%
first_row = reviews[0] # Raises KeyError

q3.assert_check_failed()

#%%RM_IF(PROD)%%
first_row = reviews.iloc[0]

q3.assert_check_passed()

#_COMMENT_IF(PROD)_
q3.hint()
#_COMMENT_IF(PROD)_
q3.solution()

"""
Explanation: 3.
Select the first row of data (the first record) from reviews, assigning it to the variable first_row.
End of explanation
"""


first_descriptions = ____

# Check your answer
q4.check()
first_descriptions

#%%RM_IF(PROD)%%
first_descriptions = desc.head(10)

q4.assert_check_passed()

#%%RM_IF(PROD)%%
first_descriptions = desc[:10]

q4.assert_check_passed()

#%%RM_IF(PROD)%%
first_descriptions = desc.iloc[:10]

q4.assert_check_passed()

#%%RM_IF(PROD)%%
first_descriptions = reviews.loc[:9, "description"]

q4.assert_check_passed()

#%%RM_IF(PROD)%%
first_descriptions = reviews.loc[:9, "description"]

q4.assert_check_passed()

#%%RM_IF(PROD)%%
first_descriptions = desc.iloc[1:10]

q4.assert_check_failed()

#_COMMENT_IF(PROD)_
q4.hint()
#_COMMENT_IF(PROD)_
q4.solution()

"""
Explanation: 4.
Select the first 10 values from the description column in reviews, assigning the result to variable first_descriptions.
Hint: format your output as a pandas Series.
End of explanation
"""


sample_reviews = ____

# Check your answer
q5.check()
sample_reviews

#%%RM_IF(PROD)%%
indices = [1, 2, 3, 5, 8]
sample_reviews = reviews.loc[indices]

q5.assert_check_passed()

#%%RM_IF(PROD)%%
indices = [1, 2, 3, 5, 8]
sample_reviews = reviews.iloc[indices]

q5.assert_check_passed()

#_COMMENT_IF(PROD)_
q5.hint()
#_COMMENT_IF(PROD)_
q5.solution()

"""
Explanation: 5.
Select the records with index labels 1, 2, 3, 5, and 8, assigning the result to the variable sample_reviews.
In other words, generate the following DataFrame:
End of explanation
"""


df = ____

# Check your answer
q6.check()
df

#%%RM_IF(PROD)%%
cols = ['country', 'province', 'region_1', 'region_2']
indices = [0, 1, 10, 100]
df = reviews.loc[indices, cols]

q6.assert_check_passed()

#_COMMENT_IF(PROD)_
q6.hint()
#_COMMENT_IF(PROD)_
q6.solution()

"""
Explanation: 6.
Create a variable df containing the country, province, region_1, and region_2 columns of the records with the index labels 0, 1, 10, and 100. In other words, generate the following DataFrame:
End of explanation
"""


df = ____

# Check your answer
q7.check()
df

#%%RM_IF(PROD)%%
cols = ['country', 'variety']
df = reviews.head(100).loc[:,cols]

q7.assert_check_passed()

#%%RM_IF(PROD)%%
cols = ['country', 'variety']
df = reviews.loc[:99, cols]

q7.assert_check_passed()

#%%RM_IF(PROD)%%
cols_idx = [0, 11]
df = reviews.iloc[:100, cols_idx]

q7.assert_check_passed()

#_COMMENT_IF(PROD)_
q7.hint()
#_COMMENT_IF(PROD)_
q7.solution()

"""
Explanation: 7.
Create a variable df containing the country and variety columns of the first 100 records. 
Hint: you may use loc or iloc. When working on the answer this question and the several of the ones that follow, keep the following "gotcha" described in the tutorial:

iloc uses the Python stdlib indexing scheme, where the first element of the range is included and the last one excluded. 
loc, meanwhile, indexes inclusively. 
This is particularly confusing when the DataFrame index is a simple numerical list, e.g. 0,...,1000. In this case df.iloc[0:1000] will return 1000 entries, while df.loc[0:1000] return 1001 of them! To get 1000 elements using loc, you will need to go one lower and ask for df.iloc[0:999].
End of explanation
"""


italian_wines = ____

# Check your answer
q8.check()

#%%RM_IF(PROD)%%
italian_wines = reviews[reviews.country == 'Italy']

q8.assert_check_passed()

#_COMMENT_IF(PROD)_
q8.hint()
#_COMMENT_IF(PROD)_
q8.solution()

"""
Explanation: 8.
Create a DataFrame italian_wines containing reviews of wines made in Italy. Hint: reviews.country equals what?
End of explanation
"""


top_oceania_wines = ____

# Check your answer
q9.check()
top_oceania_wines

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

q9.assert_check_passed()

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

q9.assert_check_passed()

#%%RM_IF(PROD)%%
top_oceania_wines = reviews[
    (reviews.country == 'Australia')
    & (reviews.points >= 95)
]

q9.assert_check_failed()

#%%RM_IF(PROD)%%
top_oceania_wines = reviews[
    (reviews.country == 'Australia')
    | (reviews.country == 'New Zealand')
    & (reviews.points >= 95)
]

q9.assert_check_failed()

#%%RM_IF(PROD)%%
top_oceania_wines = reviews[
    ((reviews.country == 'Australia')
    | (reviews.country == 'New Zealand'))
    & (reviews.points >= 95)
]

q9.assert_check_passed()

#_COMMENT_IF(PROD)_
q9.hint()
#_COMMENT_IF(PROD)_
q9.solution()

"""
Explanation: 9.
Create a DataFrame top_oceania_wines containing all reviews with at least 95 points (out of 100) for wines from Australia or New Zealand.
End of explanation
"""


