我有一个名为pandas的DataFrame对象xiv,它有一列int64Volume测量值.
In[]: xiv['Volume'].head(5)
Out[]:
0 252000
1 484000
2 62000
3 168000
4 232000
Name: Volume, dtype: int64
Run Code Online (Sandbox Code Playgroud)
我已经阅读了其他帖子(比如这个和这个),提出了以下解决方案.但是,当我使用任何一种方法时,它似乎不会改变dtype底层数据:
In[]: xiv['Volume'] = pd.to_numeric(xiv['Volume'])
In[]: xiv['Volume'].dtypes
Out[]:
dtype('int64')
Run Code Online (Sandbox Code Playgroud)
要么...
In[]: xiv['Volume'] = pd.to_numeric(xiv['Volume'])
Out[]: ###omitted for brevity###
In[]: xiv['Volume'].dtypes
Out[]:
dtype('int64')
In[]: xiv['Volume'] = xiv['Volume'].apply(pd.to_numeric)
In[]: xiv['Volume'].dtypes
Out[]:
dtype('int64')
Run Code Online (Sandbox Code Playgroud)
我也尝试制作一个单独的pandas Series并使用上面列出的方法在该系列上并重新分配给x['Volume']obect,这是一个pandas.core.series.Series对象.
但是,我已经使用numpy包的float64类型找到了解决这个问题的方法- 这有效,但我不知道它为什么会有所不同.
In[]: xiv['Volume'] = xiv['Volume'].astype(np.float64)
In[]: xiv['Volume'].dtypes
Out[]:
dtype('float64') …Run Code Online (Sandbox Code Playgroud)