3

I have the following dataframe:

import pandas as pd
mc_all = pd.DataFrame({'col_1': [0,1,1,2],
'col_2' : ['france','france','uk','uk']})

I am trying this in order to save this dataframe into a sav file

args = ( list(mc_all.columns), dict(zip(list(mc_all.columns),[0]*len(list(mc_all.columns)))) )
with SavWriter('mc_all.sav',*args) as writer:
    writer.writerows(mc_all)

according to this but it doesnt work. it throws an error:

error.SPSSIOError: Problem committing header [SPSS_INVALID_HANDLE]

Any ideas ?

eli-k
  • 10,898
  • 11
  • 40
  • 44
quant
  • 4,062
  • 5
  • 29
  • 70

2 Answers2

6

You can use pyreadstat:

import pyreadstat

pyreadstat.write_sav(mc_all, savFileName)

More information here:

https://github.com/Roche/pyreadstat#writing-files

Otto Fajardo
  • 3,037
  • 1
  • 18
  • 26
2

.writerows can't take as input. You have to convert your dataframe to np.array to write it.

import pandas as pd
import numpy as np

mc_all = pd.DataFrame({'col_1': [0,1,1,2],
                       'col_2' : ['france','france','uk','uk']})
savFileName = 'mc_all.sav'
args = (['col_1', 'col_2'], dict(col_1=0, col_2=0))
array = mc_all.values
with SavWriter(savFileName, *args) as writer:
    writer.writerows(array)
Manu
  • 178
  • 6
  • where is `SavWriter` imported from? I'm guessing it's this package : https://pythonhosted.org/savReaderWriter/#module-savReaderWriter , but you've not imported it. Why have you imported pandas and numpy but not savwriter? I feel i'm missing something obvious... – baxx Sep 30 '19 at 17:11