해설

1. 'A' column이 모두 2배

df['A'] = df['A'] * 2

2. 'B'에서 'E'까지의 column은 80보다 큰 것은 1, 작은 것은 0

# 80보다 작은 값은 0으로 변경
lowers = df.loc[:, 'B':'E'] < 80
df[lowers] = 0

# 80 이상의 값은 1로 변경
highers = df.loc[:, 'B':'E'] >= 80
df[highers] = 1

 

df[df.loc[:, 'B':'E'] < 80] = 0
df[df.loc[:, 'B':'E'] >= 80] = 1

3. 2번 index의 'F' column 값이 29에서 99

df.loc[2, 'F'] = 99

 

모범 답안

import pandas as pd

df = pd.read_csv('data/Puzzle_before.csv')

df['A'] = df['A'] * 2
df[df.loc[:, 'B':'E'] < 80] = 0
df[df.loc[:, 'B':'E'] >= 80] = 1
df.loc[2, 'F'] = 99

# 테스트 코드
df

 

  1. ID 1의 무게를 200으로 변경.
  2. ID 21의 row를 삭제
  3. ID 20의 row를 추가. ID 20의 키는 70, 무게는 200
 
 

해설

1. ID 1의 무게를 200으로 변경

import pandas as pd
df = pd.read_csv('data/body_imperial1.csv', index_col=0)

 

df.loc[1,"Weight (Pound)"] = 200

 

2. ID 21의 row를 삭제

df.drop(21, axis="index", inplace=True)

3. ID 20의 row를 추가. ID 20의 키는 70, 무게는 200

ID 20의 row를 인덱싱한 후, 키와 무게를 리스트에 넣어서 전달해줍니다.

df.loc[20] = [70,200]

 

모범 답안

import pandas as pd
df = pd.read_csv('data/body_imperial1.csv', index_col=0)

# 데이터 고치기
df.loc[1,"Weight (Pound)"] = 200
df.drop(21, axis = "index", inplace = True)
df.loc[20] = [70,200]

# 테스트 코드
df

모범 답안

# 데이터 읽기
import pandas as pd
df = pd.read_csv('data/body_imperial2.csv', index_col=0)

# 데이터 고치기
df["비만도"] = "정상"
df.loc[:10,"Gender"] = "Male"
df.loc[11:,"Gender"] = "Female"

# 정답 출력
df

모범 답안

import pandas as pd

df = pd.read_csv('data/toeic.csv')

pass_total = df['LC'] + df['RC'] > 600
pass_both = (df['LC'] >= 250) & (df['RC'] >= 250)
df['합격 여부'] = pass_total & pass_both

# 테스트 코드
df

What are blue-green algae?

Though often referred to as algae, blue-green algae are not algae at all, but types of bacteria called cyanobacteria. They are normally present in bodies of water. This type of bacteria thrives in warm, nutrient-rich water. When conditions are right, the blue-green algae can grow quickly forming “blooms.” Certain varieties of blue-green algae can produce toxins that are linked to illness in humans and animals.

What do blue-green algal blooms look like?

Blue-green algal blooms are often described as looking like pea soup or spilled green paint. However, blooms aren’t always large and dense and can sometimes cover small portions of the lake with little visible algae present. Blooms can also produce a swampy odor when the cells break down.

What are harmful algal blooms?

Blue-green algae blooms are harmful when they produce toxins that can make humans and animals sick. Most blooms are not harmful. You can't tell by looking at a bloom if it is harmful or not.

When do harmful algal blooms occur?

Blue-green algae prefer warm, calm, sunny weather and water temperatures higher than 75°F. Blooms usually occur during summer and early fall, but can occur other times of the year, if conditions are right. 

What are the possible health effects?

You can become sick if you swallow, have skin contact with, or breathe in airborne water droplets while swimming, boating, waterskiing, tubing, bathing, or showering in water that has harmful algae or if you drink water that contains algal toxins. If you become sick, you might experience vomiting, diarrhea, rash, eye irritation, cough, sore throat, and headache. Symptoms generally begin hours to two days after exposure.

 

- Buildings are estimated to account for approximately 40% of primary energy and 36% of greenhouse
emissions.

- Data on the total building stock in European Member States is reported in Figure 2 together with the number of new dwellings divided per typology.

 

- Data on energy consumption of the existing stock show that the largest energy saving potential is associated with the older building stock characterized by a lack of building
envelope insulation.

- The predominant energy end-use is space heating which is responsible for about
70% of dwelling consumption.

- Electricity consumption per household has been decreasing in most countries since 2008 thanks to the diffusion of efficient appliances,
compact fluorescent lights and light emitting diodes.

- Directives aimed at the improvement of buildings energy performance are the Energy Efficiency Directive (EED) (EU, 2012/27/EU) and the Renewable Energy Directive (RED) (EU, 2009/28/EU)

- According to the EPBD recast, new buildings occupied by public authorities and
properties have to be NZEBs by December 31, 2018 and all new buildings by December 31,
2020 [17]

- The nearly zero or very low amount of energy required should be

covered to a very significant extent by energy from renewable sources produced on-site or nearby.

 

- Primary energy factors used for the determination of the
primary energy use may be based on national or regional yearly average values taking into
account relevant European standards.

- Several studies have shown how a heterogeneous situation characterizes Europe in
relation to building and climate types. As a consequence, different cost-optimal
levels and packages of energy efficient measures can be found .

해설

import pandas as pd

df = pd.read_csv('data/broadcast.csv', index_col=0)

 

DataFrame에서 인덱싱을 통해 값을 받아오기 위해서는 loc 메소드

df.loc[row, column]
import pandas as pd

df = pd.read_csv('data/broadcast.csv', index_col=0)
df.loc[2016, 'KBS']
27.583000000000002

해설

column에 대한 인덱싱은 두 가지 방식

df[column]

 

모범 답안

import pandas as pd

df = pd.read_csv('data/broadcast.csv', index_col=0)
df['JTBC']
2011    7.380
2012    7.878
2013    7.810
2014    7.490
2015    7.267
2016    7.727
2017    9.453
Name: JTBC, dtype: float64

해설

column에 대한 인덱싱은 두 가지 방식

df[column]

 

리스트로 인덱싱

df[[column1, column2]]

우리가 원하는 column은 'SBS'와 'JTBC' 이니까, 다음과 같이 코드를 작성하면 됩니다.

모범 답안

import pandas as pd

df = pd.read_csv('data/broadcast.csv', index_col=0)
df[['SBS', 'JTBC']]

해설

1. 데이터 파악하기

import pandas as pd

samsong_df = pd.read_csv('data/samsong.csv')
samsong_df

import pandas as pd

hyundee_df = pd.read_csv('data/hyundee.csv')
hyundee_df

samsong_df['요일']
samsong_df['문화생활비']
hyundee_df['문화생활비']

2. 파이썬 사전(dictionary) 만들기

세 개의 column 'day', 'samsong', 'hyundee' 

{'day': samsong_df['요일'], 
    'samsong': samsong_df['문화생활비'], 
    'hyundee': hyundee_df['문화생활비']}

모범 답안

import pandas as pd

samsong_df = pd.read_csv('data/samsong.csv')
hyundee_df = pd.read_csv('data/hyundee.csv')

combined_df = pd.DataFrame({
    'day': samsong_df['요일'], 
    'samsong': samsong_df['문화생활비'], 
    'hyundee': hyundee_df['문화생활비']
})
combined_df
   day  samsong  hyundee
0  MON     4308     5339
1  TUE     7644     3524
2  WED     5674     5364
3  THU     8621     9942
4  FRI    23052    33511
5  SAT    15330    19397
6  SUN    19030    19925

데이터프레임 슬라이싱 

import pandas as pd

df = pd.read_csv('data/broadcast.csv', index_col=0)
# 여기에 코드를 작성하세요
df = df.loc[2012:2017, 'KBS':'SBS']
df

불린 인덱싱

import pandas as pd

df = pd.read_csv('data/broadcast.csv', index_col=0)
# 여기에 코드를 작성하세요
condition = df['SBS']<df['TV CHOSUN']
df = df.loc[condition]
df = df[['SBS','TV CHOSUN']]
df

DataFrame 인덱싱을 하는 방법

하나의 row 이름 df.loc["row4"]  
row 이름의 리스트 df.loc[["row4", "row5", "row3"]]  
row 이름의 리스트 슬라이싱 df.loc["row2":"row5"] df["row2":"row5"]
하나의 column 이름 df.loc[:, "col1"] df["col1"]
column 이름의 리스트 df.loc[:, ["col4", "col6", "col3"]] df[["col4", "col6", "col3"]]
column 이름의 리스트 슬라이싱 df.loc[:, "col2":"col5"]  
위치로 인덱싱하기기본 형태단축 형태
하나의 row 위치 df.iloc[8]  
row 위치의 리스트 df.iloc[[4, 5, 3]]  
row 위치의 리스트 슬라이싱 df.iloc[2:5] df[2:5]
하나의 column 위치 df.iloc[:, 3]  
column 위치의 리스트 df.iloc[:, [3, 5, 6]]  
column 위치의 리스트 슬라이싱 df.iloc[:, 3:7]  
import pandas as pd

df = pd.read_csv('data/body_imperial1.csv', index_col=0)

# 여기에 코드를 작성하세요
df = df.index
iname = df.index.name

 

Draw Date가 0번 column에 있으니, index_col=0

import pandas as pd

df = pd.read_csv('data/mega_millions.csv', index_col=0)
df # 테스트 코드

몇 번째 Column인지 명확하지 않을 때는 숫자 0 대신 index_col='Draw Date' 칼럼 이름 직접입력

import pandas as pd

df = pd.read_csv('data/mega_millions.csv', index_col='Draw Date')
df # 테스트 코드

+ Recent posts