2014-02-19 73 views
1

我有一個大小爲153895行x 644列(從csv文件讀取)的熊貓數據框,並且有幾列是字符串,其他是整數和浮點數。我試圖將其保存爲Rda文件。convert_to_r_dataframe給出錯誤無屬性dtype

我想:

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

我可以重現該錯誤消息時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' 
    

如果這確實是你的問題的根源,你可以通過重命名列獨特的東西修復:

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

涼爽。謝謝,讓我看看。 – uday

+0

是的,這是問題所在 – uday

相關問題