pandans_DataFrame函数
2023-02-02 本文已影响0人
敬子v
探索DataFrame函数
步骤1 导入必要的库
import pandas as pd
步骤2 创建一个数据字典
raw_data={"name": ['Bulbasaur', 'Charmander','Squirtle','Caterpie'],
"evolution": ['Ivysaur','Charmeleon','Wartortle','Metapod'],
"type": ['grass', 'fire', 'water', 'bug'],
"hp": [45, 39, 44, 45],
"pokedex": ['yes', 'no','yes','no'] }
步骤3 将数据字典存为一个名叫pokemon的数据框中
pokemon=pd.DataFrame(raw_data)
print(pokemon.head())
步骤4 数据框的列排序是字母顺序,请重新修改为name, type, hp, evolution, pokedex这个顺序
pokemon=pokemon[['name','type','hp','evolution','pokedex']]
print(pokemon.head())
步骤5 添加一个列place
pokemon['place']=['park','street','lake','forest']
print(pokemon.head(4))
步骤6 查看每个列的数据类型
print(pokemon.dtypes)
# 步骤3
name evolution type hp pokedex
0 Bulbasaur Ivysaur grass 45 yes
1 Charmander Charmeleon fire 39 no
2 Squirtle Wartortle water 44 yes
3 Caterpie Metapod bug 45 no
# 步骤4
name type hp evolution pokedex
0 Bulbasaur grass 45 Ivysaur yes
1 Charmander fire 39 Charmeleon no
2 Squirtle water 44 Wartortle yes
3 Caterpie bug 45 Metapod no
# 步骤5
name type hp evolution pokedex place
0 Bulbasaur grass 45 Ivysaur yes park
1 Charmander fire 39 Charmeleon no street
2 Squirtle water 44 Wartortle yes lake
3 Caterpie bug 45 Metapod no forest
# 步骤6
name object
type object
hp int64
evolution object
pokedex object
place object
dtype: object