[부스트코스] 코칭스터디 9기 : AI Basic 2023 4주차 미션(Q1, Q2, Q3) 변형 문제 풀이

2023. 2. 14. 23:39BOOTCAMP/boostcourse AI BASIC 2023

 
Q1. Pandas의 Series 형태로 만들어보세요.
 
import pandas as pd

idx = ["Banana", "Apple", "Kiwi", "Tomato"]
data = [34, 23, 6, 88]

#위 데이터로 Series를 구현해보세요.
series = pd.Series(data, index=idx)

# 10 이상 20 이하를 가지는 데이터만 이용해 다시 series를 정의하세요.
series = series[series >= 10][series <= 20]

print(series)

Banana   34
Apple    23
dtype: int64

 

Q2. 각 표에 정리된 데이터를 각각 하나의 데이터 프레임으로 생성한 후 다음 세부 구현을 진행해보세요.
import pandas as pd

df1 = pd.DataFrame([
                   ["cherry", "Fruit", 100],
                   ["mango", "Fruit", 110],
                   ["potato", "Vegetable", 60],
                   ["onion", "Vegetable", 80]],
                   columns=["Name", "Type", "Price"])

df2 = pd.DataFrame([
                   ["pepper", "Vegetable", 50],
                   ["carrot", "Vegetable", 70],
                   ["banana", "Fruit", 90],
                   ["kiwi", "Fruit", 120]],
                   columns=["Name", "Type", "Price"])

#df1, df2를 colums를 이용해 결합
df3 = pd.concat([df1, df2], axis=0) 

# Fruit와 Vegetable의 type에 따라 정렬하고, 가격을 내림차순으로 정리 
df_fruit = df3.loc[df3["Type"] == "Fruit"]
df_fruit = df_fruit.sort_values(by="Price", ascending=False)
print(df_fruit)

df_veg = df3.loc[df3["Type"] == "Vegetable"]
df_veg = df_veg.sort_values(by="Price", ascending=False)
print(df_veg)

# Fruit와 Vegetable 상위 2개의 가격의 합을 출력 
print("Sum of Top 2 Fruit Price : ", sum(df_fruit[:2]["Price"])) 
print("Sum of Top 2 Vegetable Price : ", sum(df_veg[:2]["Price"]))

     Name   Type  Price
3    kiwi  Fruit    120
1   mango  Fruit    110
0  cherry  Fruit    100
2  banana  Fruit     90
     Name       Type  Price
3   onion  Vegetable     80
1  carrot  Vegetable     70
2  potato  Vegetable     60
0  pepper  Vegetable     50
Sum of Top 2 Fruit Price :  230
Sum of Top 2 Vegetable Price :  150
Q3. dataframe의 형태로 만든 후 각 세부 구현을 진행해보세요.
import pandas as pd

idx = ["Sue", "Ryan", "Jay", "Jane", "Anna"]
col = ["round_1", "round_2", "round_3", "round_4", "round_5"] 
data = [[55, 65, 60, 66, 57],
       [64, 77, 71, 79, 67],
       [88, 81, 79, 89, 77],
       [45, 35, 30, 46, 47],
       [91, 96, 90, 97, 99]]

#위 데이터를 이용해 dataframe을 구성해보세요.
df = pd.DataFrame(data, idx, col)

#df에 새로운 column인 round_6의 데이터 [11, 15, 13, 17, 19]를 추가해보세요. 
df["round6"] = [11, 15, 13, 17, 19]
print(df)

#각 데이터의 mean, max, min 값을 구해 출력해보세요. 
print(df.describe().loc[["mean", "max", "min"]])

      round_1  round_2  round_3  round_4  round_5  round6
Sue        55       65       60       66       57      11
Ryan       64       77       71       79       67      15
Jay        88       81       79       89       77      13
Jane       45       35       30       46       47      17
Anna       91       96       90       97       99      19
      round_1  round_2  round_3  round_4  round_5  round6
mean     68.6     70.8     66.0     75.4     69.4    15.0
max      91.0     96.0     90.0     97.0     99.0    19.0
min      45.0     35.0     30.0     46.0     47.0    11.0