100일의 코드: 2022년을 위한 완전한 Python Pro 부트캠프 - 25일차(미국 주 게임)

22042 단어
  • Python에서 csv 데이터 및 Panda 라이브러리 작업

  • main.py

    # with open("weather_data.csv") as data_file:
    #     data = data_file.readlines()
    #     print(data)
    
    # import csv
    
    # with open("weather_data.csv") as data_file:
    #     data = csv.reader(data_file)
    #     for row in data:
    #         print(row)
    
    # with open("weather_data.csv") as data_file:
    #     data = csv.reader(data_file)
    #     temperatures = []
    #     for row in data:
    #         if row[1] != "temp":
    #             temperatures.append(int(row[1]))
    #
    #     print(temperatures)
    
    import pandas
    
    # data = pandas.read_csv("weather_data.csv")
    # print(type(data))
    # print(type(data["temp"]))
    
    # data_dict = data.to_dict()
    # # print(data_dict)
    #
    # temp_list = data["temp"].to_list()
    # # print(len(temp_list))
    #
    # average = sum(temp_list) / len(temp_list)
    # print(average)
    #
    # print(data["temp"].mean())
    #
    # print(data["temp"].max())
    #
    # # Get data in columns
    # print(data["condition"])
    # print(data.condition)
    
    # Get data in the row
    # print(data[data.day == "Monday"])
    # print(data[data.temp == data.temp.max()])
    
    # monday = data[data.day == "Monday"]
    # print(monday.condition)
    #
    # monday = data[data.day == "Monday"]
    # print(monday.temp)
    #
    # monday = data[data.day == "Monday"]
    # temp_conv = (monday.temp * 1.8 + 32)
    # print(temp_conv)
    
    # Create a data frame from scratch
    data_dict = {
        "students": ["Amy", "James", "Angela"],
        "scores": [76, 56, 65]
    }
    
    data = pandas.DataFrame(data_dict)
    # print(data)
    data.to_csv("new_data.csv")
    
    


    weather_data.csv

    day,temp,condition
    Monday,12,Sunny
    Tuesday,14,Rain
    Wednesday,15,Rain
    Thursday,14,Cloudy
    Friday,21,Sunny
    Saturday,22,Sunny
    Sunday,24,Sunny
    


    new_data.csv

    ,students,scores
    0,Amy,76
    1,James,56
    2,Angela,65
    


  • 판다를 사용한 대다람쥐 조사 데이터

  • main.py

    # How many coloured squirrels per color
    # Create a new csv with this data (squirrel_count.csv)
    
    import pandas
    
    data = pandas.read_csv("2018_Central_Park_Squirrel_Census_Squirrel_Data.csv")
    gray_squirrels_count = len(data[data["Primary Fur Color"] == "Gray"])
    red_squirrels_count = len(data[data["Primary Fur Color"] == "Cinnamon"])
    black_squirrels_count = len(data[data["Primary Fur Color"] == "Black"])
    print(gray_squirrels_count)
    print(red_squirrels_count)
    print(black_squirrels_count)
    
    data_dict = {
        "Fur Color": ["Gray", "Cinnamon", "Black"],
        "Count": [gray_squirrels_count, red_squirrels_count, black_squirrels_count]
    }
    
    data_frame = pandas.DataFrame(data_dict)
    data_frame.to_csv("squirrel_count.csv")
    


    squirrel_count.csv

    ,Fur Color,Count
    0,Gray,2473
    1,Cinnamon,392
    2,Black,103
    


    2018_중앙_공원_다람쥐_Census_Squirrel_Data.csv

    # download from https://data.cityofnewyork.us/Environment/2018-Central-Park-Squirrel-Census-Squirrel-Data/vfnx-vebw
    


  • 미국 주 게임

  • main.py

    import turtle
    import pandas
    
    screen = turtle.Screen()
    screen.title("U.S States Game")
    image = "blank_states_img.gif"
    screen.addshape(image)
    turtle.shape(image)
    
    data = pandas.read_csv("50_states.csv")
    all_states = data.state.to_list()
    guessed_states = []
    
    while len(guessed_states) < 50:
        answer_state = screen.textinput(title=f"{len(guessed_states)}/50 States Correct",
                                        prompt="Whats another state's name?").title()
        if answer_state == "Exit":
            missing_states = []
            for state in all_states:
                if state not in guessed_states:
                    missing_states.append(state)
            new_data = pandas.DataFrame(missing_states)
            new_data.to_csv("states_to_learn.csv")
            break
        if answer_state in all_states:
            guessed_states.append(answer_state)
            t = turtle.Turtle()
            t.hideturtle()
            t.penup()
            state_date = data[data.state == answer_state]
            t.goto(int(state_date.x), int(state_date.y))
            t.write(answer_state)
    


  • 50_States.csv

  • state,x,y
    Alabama,139,-77
    Alaska,-204,-170
    Arizona,-203,-40
    Arkansas,57,-53
    California,-297,13
    Colorado,-112,20
    Connecticut,297,96
    Delaware,275,42
    Florida,220,-145
    Georgia,182,-75
    Hawaii,-317,-143
    Idaho,-216,122
    Illinois,95,37
    Indiana,133,39
    Iowa,38,65
    Kansas,-17,5
    Kentucky,149,1
    Louisiana,59,-114
    Maine,319,164
    Maryland,288,27
    Massachusetts,312,112
    Michigan,148,101
    Minnesota,23,135
    Mississippi,94,-78
    Missouri,49,6
    Montana,-141,150
    Nebraska,-61,66
    Nevada,-257,56
    New Hampshire,302,127
    New Jersey,282,65
    New Mexico,-128,-43
    New York,236,104
    North Carolina,239,-22
    North Dakota,-44,158
    Ohio,176,52
    Oklahoma,-8,-41
    Oregon,-278,138
    Pennsylvania,238,72
    Rhode Island,318,94
    South Carolina,218,-51
    South Dakota,-44,109
    Tennessee,131,-34
    Texas,-38,-106
    Utah,-189,34
    Vermont,282,154
    Virginia,234,12
    Washington,-257,193
    West Virginia,200,20
    Wisconsin,83,113
    Wyoming,-134,90
    


    blank_states.img

    좋은 웹페이지 즐겨찾기