Pandas has two primary concepts:

  1. DataFrame → like a sheet(table)
  2. Series → a single column from dataframe

Pandas API Reference Doc:https://pandas.pydata.org/docs/reference/

If your want to calculation sheet(table) column, you can use pandas series method mean().

import pandas
data = pandas.read_csv('weather_data.csv')
temp_avg = data['temp'].mean()

Calculate List Average Multiple Method:

def Average(lst):
    # method 1
    return sum(lst) / len(lst)

    # method 2
    from functools import reduce
    return reduce(lambda a, b: a + b, lst) / len(lst)

    # method 3
    from statistics import mean
    return mean(lst)

    # method 4
    import numpy as np
    return np.mean(lst)

When you want to call Pandas Series(column) with name

  1. data['temp'] → like dictionary
  2. data.temp → pandas turn col name into attribute

25-1 Calculate Counts of Different Squirrel's Fur Color

Code Implementation (Total 3 method, and not contain teacher's method):

import pandas as pd

squirrel_data = pd.read_csv('2018_Central_Park_Squirrel_Census_-_Squirrel_Data.csv')

grey_count = squirrel_data[squirrel_data['Primary Fur Color'] == 'Gray']['Primary Fur Color'].count()
red_count = squirrel_data[squirrel_data['Primary Fur Color'] == 'Cinnamon']['Primary Fur Color'].count()
black_count = squirrel_data[squirrel_data['Primary Fur Color'] == 'Black']['Primary Fur Color'].count()

# Method 1:
squirrel_fur_info = [{'Fur Color': 'grey', 'Count': grey_count}, {'Fur Color': 'red', 'Count': red_count},
                     {'Fur Color': 'black', 'Count': black_count}]
squirrel_df = pd.DataFrame(squirrel_fur_info)
print(squirrel_df)
print()

# Method 2:
# Add By a Single Row
squirrel_fur_info_2 = [['grey', grey_count], ['red', red_count], ['black', black_count]]
squirrel_df_2 = pd.DataFrame(squirrel_fur_info_2, columns=['Fur Color', 'Count'])
print(squirrel_df_2)
print()

# Method 3:
# Add By a Single Column
squirrel_fur_info_3 = [['grey', 'red', 'black'], [grey_count, red_count, black_count]]
squirrel_df_3 = pd.DataFrame(squirrel_fur_info_3).transpose()
squirrel_df_3.columns = ['Fur Color', 'Count']
print(squirrel_df_3)
print()

25-2 Guess U.S. States Game

Code Implementation (Not contain teacher's method):

import turtle
import pandas as pd
from move import MoveCorrectState

IMAGE = "blank_states_img.gif"

screen = turtle.Screen()
screen.title('U.S. States Game')
screen.addshape(IMAGE)
turtle.shape(IMAGE)

states_data = pd.read_csv('50_states.csv')
states_lst = states_data.state.to_list()
is_game_continue = True

while is_game_continue:
    corrected_state = len(states_data.state) - len(states_lst)
    answer = screen.textinput(title=f"{corrected_state}/{len(states_data.state)} States Correct", prompt="What's another states name?").title()
    if answer == 'Exit':
        output_df = pd.DataFrame(states_lst, columns=['states'])
        output_df.to_csv('states_to_learn.csv')
        break
    if answer in states_lst:
        ans = MoveCorrectState()
        x = float(states_data[states_data.state == answer].x)
        y = float(states_data[states_data.state == answer].y)
        ans.write_state(x, y, answer)
        states_lst.remove(answer)

    if len(states_lst) == 0:
        end = MoveCorrectState()
        end.win()
        is_game_continue = False

screen.exitonclick()
from turtle import Turtle

ALIGN = "center"
FONT = ("Courier", 8, "normal")

class MoveCorrectState(Turtle):
    def __init__(self):
        super().__init__()
        self.hideturtle()
        self.penup()

    def write_state(self, x, y, text):
        self.goto(x, y)
        self.write(f"{text}", align=ALIGN, font=FONT)

    def win(self):
        self.color('green')
        self.goto(0, 0)
        self.write(f"YOU WIN!!", align='center', font=("Courier", 28, "normal"))