相关疑难解决方法(0)

何时申请(pd.to_numeric)以及何时在python中使用astype(np.float64)?

我有一个名为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)

python types numpy dataframe pandas

30
推荐指数
1
解决办法
4万
查看次数

标签 统计

dataframe ×1

numpy ×1

pandas ×1

python ×1

types ×1