基于DataFrame改变列类型的方法
作者:无痛学习 时间:2021-05-30 05:36:42
今天用numpy 的linalg.det()求矩阵的逆的过程中出现了一个错误:
TypeError: No loop matching the specified signature and casting was found for ufunc det
查了半天发现是数据类型的问题,numpy在算逆的时候会先检查一下数据类型是否一致,若不一致就会报错(话说这个错误提示信息也太难理解了,还得看源码o(╯□╰)o)。
由于我的数据是用pandas.DataFrame读取的,所以每一列的数据类型有可能不同。
回头检查一下数据,果然有的是int,有的是float。所以全部改为float64类型。
找到了如下的方法,以及DataFrame数据类型:
DataFrame 类型转换方法—astype()
import pandas as pd
df = pd.DataFrame([{'col1':'a', 'col2':'1'}, {'col1':'b', 'col2':'2'}])
print df.dtypes
df['col2'] = df['col2'].astype('int')
print '-----------'
print df.dtypes
df['col2'] = df['col2'].astype('float64')
print '-----------'
print df.dtypes
输出:
col1 object
col2 object
dtype: object
-----------
col1 object
col2 int32
dtype: object
-----------
col1 object
col2 float64
dtype: object
astype()也能一次改变所有数据的类型:
In[30]:a
Out[31]:
a b c d
0 0.891380 0.442167 -0.539450 1.023458
1 -0.488131 -1.847104 -0.209799 -0.768713
2 1.290434 0.327096 0.358406 0.422209
In[32]:a.astype('int32')
Out[32]:
a b c d
0 0 0 0 1
1 0 -1 0 0
2 1 0 0 0
附:data type list
Data type Description
bool_ Boolean (True or False) stored as a byte
int_ Default integer type (same as C long; normally either int64 or int32)
intc Identical to C int (normally int32 or int64)
intp Integer used for indexing (same as C ssize_t; normally either int32 or int64)
int8 Byte (-128 to 127)
int16 Integer (-32768 to 32767)
int32 Integer (-2147483648 to 2147483647)
int64 Integer (-9223372036854775808 to 9223372036854775807)
uint8 Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
float_ Shorthand for float64.
float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_ Shorthand for complex128.
complex64 Complex number, represented by two 32-bit floats (real and imaginary components)
complex128 Complex number, represented by two 64-bit floats (real and imaginary components)
来源:https://blog.csdn.net/wenshen1927/article/details/76889546
标签:DataFrame,列,类型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中实现延时回调普通函数示例代码
2023-10-03 02:17:04
浅谈python正则的常用方法 覆盖范围70%以上
2022-05-18 21:01:13
![](https://img.aspxhome.com/file/2023/4/77034_0s.png)
django处理select下拉表单实例(从model到前端到post到form)
2023-09-02 13:47:09
ASP 使用Filter函数来检索数组
2011-04-30 16:49:00
ASP利用Google实现在线翻译功能
2010-03-07 17:28:00
在Dreamweaver MX中应用“占位图形”
2009-07-10 13:16:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113314790s.gif)
Python函数式编程指南(三):迭代器详解
2023-06-03 06:11:52
该用多大的字
2009-05-17 14:39:00
python实现学生成绩测评系统
2023-08-09 19:40:56
python爬虫selenium和phantomJs使用方法解析
2023-02-06 19:23:38
![](https://img.aspxhome.com/file/2023/6/69496_0s.png)
python实现从字符串中找出字符1的位置以及个数的方法
2023-06-13 20:36:10
Android应用开发中Action bar编写的入门教程
2022-01-03 02:25:18
ASP+Access系统的安全隐患及对策
2007-08-23 14:53:00
解析PHP观察者模式Observer
2023-07-08 13:38:58
![](https://img.aspxhome.com/file/2023/2/55402_0s.jpg)
Python 文本文件与csv文件的读取与写入
2021-02-10 09:57:56
asp开发中textarea常见问题
2008-04-13 06:34:00
用FrontPage制作缩略图和图片重叠效果
2007-11-18 14:45:00
python内置模块collections知识点总结
2023-10-27 19:37:08
用户是如何浏览你的网站的
2010-05-03 14:26:00
![](https://img.aspxhome.com/file/UploadPic/20105/3/eyetrack-74s.jpg)
如何编写一个基于WEB的文件查询系统?
2009-11-08 18:55:00