我有一个大小为153895行x 644列(从csv文件读取)的pandas数据框,并且有几列为字符串,其他列为整数和浮点数。我正在尝试将其另存为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
可能是什么问题?
最佳答案
当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
版画
A B
0 1 2
1 3 4
提高
AttributeError
:AttributeError: 'DataFrame' object has no attribute 'dtype'
如果这确实是问题的根源,则可以通过将列重命名为唯一的名称来解决此问题:
myDF.columns = ['col{i}'.format(i=i) for i in range(len(myDF.columns))]