convert_to_r_dataframe给出错误没有属性dtype

时间:2014-02-19 15:11:44

标签: python pandas

我有一个大小为153895行x 644列的pandas数据帧(从csv文件中读取),并且有几列是字符串,其他列是整数和浮点数。我试图将其保存为Rda文件。

我试过了:

import pandas.rpy.common as com
myDFinR = com.convert_to_r_dataframe(myDF)

我收到以下错误:

Traceback (most recent call last):
  File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\IPython\core\interactiveshell.py", line 2828, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-101-7d2a8ae98ea4>", line 1, in <module>
dDataR=com.convert_to_r_dataframe(dData)
  File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\pandas\rpy\common.py", line 305, in convert_to_r_dataframe
value_type = value.dtype.type
  File "C:\PF\WinPython-64bit-3.3.3.3\python-3.3.3.amd64\lib\site-packages\pandas\core\generic.py", line 1815, in __getattr__
(type(self).__name__, name))
AttributeError: 'DataFrame' object has no attribute 'dtype'

我试图做myDF.dtypes并且它没有给我任何不寻常的输出

col1        object
col2        object
col3        int64
...
col642      float64
col643      float64
col644      float64
Length: 644, dtype: object

当我尝试for i,j in enumerate(myDF.columns): print(i,":",myDF[j].dtype)时,它在第359行给了我一个错误。但是,如果我尝试myDF[[359]].dtypes它会给我

col359      float64
dtype: object

可能是什么问题?

1 个答案:

答案 0 :(得分:1)

myDF具有非唯一列名时,我可以重现错误消息:

import pandas as pd
import pandas.rpy.common as com

myDF = pd.DataFrame([[1,2],[3,4]], columns=['A','B'])
myDFinR = com.convert_to_r_dataframe(myDF)
print(myDFinR)   # 1

myDF2 = pd.DataFrame([[1,2],[3,4]], columns=['A','A'])
myDFinR2 = com.convert_to_r_dataframe(myDF2)
print(myDFinR2)  # 2
  1. 打印

      A B
    0 1 2
    1 3 4
    
  2. 提升AttributeError

    AttributeError: 'DataFrame' object has no attribute 'dtype'
    

  3. 如果这确实是您的问题的根源,您可以通过将列重命名为独特的内容来修复它:

    myDF.columns = ['col{i}'.format(i=i) for i in range(len(myDF.columns))]