python - Pandas:为什么在 Titanic 数据库电子表格中缺少 Column 会将 dtype 作为对象?
问题描述
我从 kaggle 获得了巨大的数据,上传到谷歌电子表格并从 colab 读取。并发现 Age Dtype 由于缺少值(或其他原因)而得到对象。如何将 Age Dtype 更改为 float64?
from google.colab import auth
import pandas as pd
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
worksheet = gc.open('titanic_train').sheet1
# get_all_values gives a list of rows.
datas = worksheet.get_all_records()
print(datas)
pd.DataFrame(datas).info()
我得到了下面的信息
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 PassengerId 891 non-null int64
1 Survived 891 non-null int64
2 Pclass 891 non-null int64
3 Name 891 non-null object
4 Sex 891 non-null object
5 Age 891 non-null object
6 SibSp 891 non-null int64
7 Parch 891 non-null int64
8 Ticket 891 non-null object
9 Fare 891 non-null float64
10 Cabin 891 non-null object
11 Embarked 891 non-null object
dtypes: float64(1), int64(5), object(6)
memory usage: 83.7+ KB
解决方案
您需要将该Age
列转换为整数数据类型。这可以按如下方式完成:
df = pd.DataFrame(datas)
df['Age'] = pd.to_numeric(df['Age'])
推荐阅读
- python - Wordnet 的属性错误:AttributeError:模块“nltk_data.corpora.wordnet”没有属性“synset”
- go - 错误模块 github.com/golang/protobuf 已弃用
- python - Scrapy 蜘蛛抓取主页但不抓取相同类别的下一页
- arm - 如何让STM32F429NI中的bootloader跳转到外部Nor Flash
- python - 如何从scrapy端点python中提取抓取的项目
- php - 在php codeigniter中使用json显示变量不起作用
- node.js - 如何将我的非输入 ejs 发送到 MongoDB Atlas
- c - 写入安全的 UART 中断缓冲区
- javascript - 如何按顺序从日期数组中获取年份
- ios - 当我按一次按钮时,键盘不显示,但是当我长时间按下一个按钮时