这十套练习,教你如何用Pandas做数据分析(08)

9 篇文章 2 订阅
9 篇文章 0 订阅

练习8-创建数据框

探索Pokemon数据

在这里插入图片描述
步骤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)
pokemon.head()
evolution hp name pokedex type
0 Ivysaur 45 Bulbasaur yes grass
1 Charmeleon 39 Charmander no fire
2 Wartortle 44 Squirtle yes water
3 Metapod 45 Caterpie no bug
步骤4 数据框的列排序是字母顺序,请重新修改为name, type, hp, evolution, pokedex这个顺序

运行以下代码

pokemon = pokemon[[‘name’, ‘type’, ‘hp’, ‘evolution’,‘pokedex’]]
pokemon
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 添加一个列place

运行以下代码

pokemon[‘place’] = [‘park’,‘street’,‘lake’,‘forest’]
pokemon
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 查看每个列的数据类型

运行以下代码

pokemon.dtypes
name object
type object
hp int64
evolution object
pokedex object
place object
dtype: object

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值