python:pokemon

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Step 1: Import the necessary libraries
import pandas as pd

# Step 2: Create a data dictionary
data = {
'evolution': ['Ivysaur', 'Charmeleon', 'Wartortle', 'Metapod'],
'hp': [45, 39, 44, 45],
'name': ['Bulbasaur', 'Charmander', 'Squirtle', 'Caterpie'],
'pokedex': ['yes', 'no', 'yes', 'no'],
'type': ['grass', 'fire', 'water', 'bug']
}

# Step 3: Assign it to a variable called df
df = pd.DataFrame(data)

# Step 4: Reorder the DataFrame columns
df = df[['name', 'type', 'hp', 'evolution', 'pokedex']]

# Step 5: Add another column called place
df['place'] = ['Pallet Town', 'Pallet Town', 'Pallet Town', 'Viridian Forest']

# Step 6: Present the type of each column
print(df.dtypes)

# BONUS: What is the average HP of the Pokemon in the DataFrame?
print("Average HP:", df['hp'].mean())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Step 1. Import the necessary libraries
import pandas as pd

# Step 3. Assign it to a variable called drinks
drinks = pd.read_csv('drinks.csv')
print(drinks.head(5))

# Step 4. Which continent drinks more beer on average
beer_consumption = drinks.groupby('continent')['beer_servings'].mean()
continent_with_most_beer = beer_consumption.idxmax()
print(f"大陆平均喝啤酒最多: {continent_with_most_beer},平均消费量:{beer_consumption.max()}")

# Step 5. For each continent print the statistics for wine consumption.
wine_stats = drinks.groupby('continent')['wine_servings'].describe()
print(wine_stats)

# Step 6. Print the mean alcoohol consumption per continent for every column
numeric_cols = drinks.select_dtypes(include=['number']).columns
mean_consumption_per_continent = drinks.groupby('continent')[numeric_cols].mean().reset_index()
print(mean_consumption_per_continent)

# Step 7. Print the median alcoohol consumption per continent for every column
numeric_cols = drinks.select_dtypes(include=['number']).columns
median_consumption_per_continent = drinks.groupby('continent')[numeric_cols].median().reset_index()
print(median_consumption_per_continent)

# Step 8. Print the mean, min and max values for spirit consumption.
spirit_stats = drinks.groupby('continent')['spirit_servings'].agg(['mean', 'min', 'max']).reset_index()
print(spirit_stats)
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2023-2025 John Doe
  • Visitors: | Views:

请我喝杯茶吧~

支付宝
微信