<aside> 💡

이론 문제

Q.1

Screenshot 2024-10-30 at 5.48.30 PM.png

Q.2 (X ⇒ ‘-’ 대신 “.”를 입력해야 한다.)

Q.3 (X ⇒ apply 함수를 사용한다.)

Q.4 (X)

Q.5 (X ⇒ append는 두 개까지만 병합 가능)

</aside>

<aside> 💡

실습 문제

# Q.1
df.fillna('unknown')
# Q.2
data_list=[{'yyyy.mm.dd' : '2000.06.28'},
           {'yyyy.mm.dd' : '2003.03.28'},
           {'yyyy.mm.dd' : '2006.08.25'} ]
df = pd.DataFrame(data_list)

def extract_month(column):
    return column.split(".")[1]
    
df['month'] = df['yyyy.mm.dd'].apply(extract_month)
df
# Q.3
def extract_year(column):
    return column.split('-')[0]

df['학번'] = df['학번'].apply(extract_year)
df['하츄핑'] = [df['하츄핑'][0], df['차차핑'][1], df['바로핑'][2]]
df = df.rename(columns = {'하츄핑' : '이름'})
df['이름'] = df['이름'].map({'A' : 1, 'B' : 2})
del df['차차핑']
del df['바로핑']
def average_grade(row):
    return (row['과목1'] + row['과목2'])/2
df['평균 점수'] = df.apply(average_grade, axis = 1)
df['과목1'] = df['과목1'].round(1)
df['과목2'] = df['과목2'].round(1)
df['평균 점수'] = df['평균 점수'].round(1)
df
# Q.4
df_list = [
    {'name': 'Alice', 'job': 'Teacher'},
    {'name': 'Bob', 'job': 'Doctor'},
    {'name': 'Charlie', 'job': 'Artist'},
    {'name': 'David', 'job': 'Teacher'},
    {'name': 'Eve', 'job': 'Scientist'},
    {'name': 'Frank', 'job': 'Engineer'},
    {'name': 'Grace', 'job': 'Artist'},
    {'name': 'Hannah', 'job': 'Teacher'}
]
df = pd.DataFrame(df_list, columns=['name', 'job'])
df_job = df.job.unique()
job_percentage = (df.job.value_counts()/8) * 100
df_job = pd.DataFrame({'job' : df_job})
df_job['percentage'] = job_percentage.values
print(df_job)
# Q.5
names_A = ["학생1", "학생2", "학생3"]
scores_A = [85, 90, 88]
names_B = ["학생4", "학생5", "학생6"]
scores_B = [78, 82, 80]

df_A = pd.DataFrame({'name' : names_A, 'score' : scores_A})
df_B = pd.DataFrame({'name' : names_B, 'score' : scores_B})

df = pd.concat([df_A, df_B], ignore_index = True)

def diff(score):
    return score - df.score.mean()
df['deviation'] = df.score.apply(diff)
df

</aside>