[캐글] Courses - Pandas(2)

HO94·2021년 6월 30일
0

캐글

목록 보기
10/17

2. Indexing, Selecting & Assigning

  • accessors
  • iloc, loc
  • set_index()
  • 데이터 선택(selection)
  • isnull(), notnull()
  • 데이터 할당(assigning)

accessors

  • data.attribute으로 data에 속해있는 속성 호출 가능
  • data['attribut']으로도 호출 가능

iloc, loc

  • 둘 모두 [행, 열]로 호출
  • :은 '모든 행/열'을 의미
  • iloc는 0부터 시작해서 자기자신을 포함하지 않지만 loc는 0부터 시작해서 자기자신을 포함
    - 0:10iloc에서는 0,...,9를 loc에서는 0,...,10를 의미
  • data.set_index('name')으로 새로운 인덱스(name)을 생성
  • data.loc[data.column name == 'value']으로 column에서 value와 일치하는 데이터만 가져올 수 있음

isnull( ), notnull( )

  • 비어있거나 비어있지 않은 값을 찾을 수 있음

assigning

  • data.['column name'] = 'value'로 columns name에 value 할당 가능

1번

Select the description column from reviews and assign the result to the variable desc

desc = reviews['description']

2번

Select the first value from the description column of reviews, assigning it to variable first_description

first_description = reviews['description'][0]

3번

Select the first row of data (the first record) from reviews, assigning it to the variable first_row

first_row = reviews.iloc[0]

4번

Select the first 10 values from the description column in reviews, assigning the result to variable first_descriptions.

first_descriptions = reviews.description.iloc[0:10]

5번

Select the records with index labels 1, 2, 3, 5, and 8, assigning the result to the variable sample_reviews

sample_reviews = reviews.iloc[[1,2,3,5,8],]

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

df = reviews.loc[[0,1,10,100], ['country', 'province', 'region_1', 'region_2']]

7번

Create a variable df containing the country and variety columns of the first 100 records.

df = reviews.loc[:99, ['country', 'variety']]

8번

Create a DataFrame italian_wines containing reviews of wines made in Italy

italian_wines = reviews.loc[reviews.country =='Italy',]

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.

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

0개의 댓글