我正在从一个DataFrame文件中读取hdf5:
import pandas as pd
store = pd.HDFStore('some_file.h5')
df= store['df']
store.close()
使用info显示:
In [11]: df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 21423657 entries, 0 to 21423656
Data columns (total 5 columns):
date datet
我的文件包含Company,RecordID,Sale etc..When列。加载文件到pandas dataframe后,我首先尝试找到它的数据类型,它列出了一些列的浮点型/整型。所以我将它们改为字符串,如下所示; data = pd.read_csv(filepath)
print(data.dtypes)
Company Code object
SiteCode int64
Product Name object
RECORD ID int64
Tank ID
示例
玩具数据表:
>>> df = pd.DataFrame({'a': ['the', 'this'], 'b': [5, 2.3], 'c': [8, 11], 'd': ['the', 7]})
产量:
>>> df
a b c d
0 the 5.0 8 the
1 this 2.3 11 7
以及:
>>> df.dtypes
a object
b
在检查dtypes DataFrame中的列时,我意识到整数列的数据类型是np.int64,但令人惊讶的是,在Unix上这等同于int,但在int上却不是。为什么他们的行为不一样?是否有一种方法可以在使用DataFrame进行比较时以相同的方式创建df.dtypes == int
下面是一些要说明的示例代码。
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: pd.__version__
Out[3]: '1.0.1'
In [4]: np.__version__
Out[4]: '1.1
在将read_csv与Pandas结合使用时,如果我希望将给定的列转换为类型,格式错误的值将中断整个操作,而不会指示违规的值。
例如,运行类似于:
import pandas as pd
import numpy as np
df = pd.read_csv('my.csv', dtype={ 'my_column': np.int64 })
将导致以错误结尾的堆栈跟踪:
ValueError: cannot safely convert passed user dtype of <i8 for object dtyped data in column
当我使用TypeError方法时,我遇到了一个带有熊猫稀疏数据帧的value_counts。我已经列出了我正在使用的软件包的版本。
对如何使这项工作有什么建议吗?
提前谢谢。此外,如果需要更多的信息,请告诉我。
Python 2.7.6 |Anaconda 1.9.1 (x86_64)| (default, Jan 10 2014, 11:23:15)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license
我有一些分类预测因子(即变量)为0& 1的pandas数据帧,以及一些数值变量。当我把它放到一个stasmodel中时:
est = sm.OLS(y, X).fit()
它抛出:
Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).
我使用df.convert_objects(convert_numeric=True)转换了DataFrame的所有数据类型
在此之后,所有数据帧变量的数据类型都显示为int32或int64。但在最后,它仍然显示dtype: object,如
我有一个熊猫DataFrame myDF,它有几个字符串列( dtype是object)和许多数字列。我尝试了以下几点:
d=pandas.HDFStore("C:\\PF\\Temp.h5")
d['test']=myDF
我得到了这个结果:
C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\pandas\io\pytables.py:2446: PerformanceWarning:
your performance may suffer as PyTables will
我想将数据帧的索引(行)从float64更改为字符串或unicode。
我以为这能行得通,但显然不行:
#check type
type(df.index)
'pandas.core.index.Float64Index'
#change type to unicode
if not isinstance(df.index, unicode):
df.index = df.index.astype(unicode)
错误消息:
TypeError: Setting <class 'pandas.core.index.Float64Index'&g