我想把一个pandas数据帧写入stata .dta文件。根据{{3}}中给出的建议,我写道:
import pandas as pd
df.to_stata(workdir+' generosity.dta')
我收到一条错误消息TypeError:'float'类型的对象没有len(),我不知道这意味着什么。
df中的大多数列都是对象,但有三列是float64。
我尝试通过rpy2跟随另一种方法(如本文Save .dta files in python中所述),但当我尝试安装它时,我收到一条错误消息“错误:试图猜测R的家但是没有r commnand路径“所以我放弃了它(我的计算机上有R但没有使用过一次)
非常感谢你。
编辑:结果如下:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-140-7a8f8bc8d446> in <module>()
1 #write the dataframe as a Stata file
----> 2 df.to_stata(workdir+group+' generosity.dta')
C:\Users\chungk\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\frame.pyc in to_stata(self, fname, convert_dates, write_index, encoding, byteorder, time_stamp, data_label)
1262 time_stamp=time_stamp, data_label=data_label,
1263 write_index=write_index)
-> 1264 writer.write_file()
1265
1266 @Appender(fmt.docstring_to_string, indents=1)
C:\Users\chungk\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\stata.pyc in write_file(self)
1245 self._write(_pad_bytes("", 5))
1246 if self._convert_dates is None:
-> 1247 self._write_data_nodates()
1248 else:
1249 self._write_data_dates()
C:\Users\chungk\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\stata.pyc in _write_data_nodates(self)
1327 if var is None or var == np.nan:
1328 var = _pad_bytes('', typ)
-> 1329 if len(var) < typ:
1330 var = _pad_bytes(var, typ)
1331 if compat.PY3:
TypeError: object of type 'float' has no len()