quant
quant

Reputation: 4482

how to save a pandas dataframe into a sav file

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 ?

Upvotes: 3

Views: 6824

Answers (2)

Otto Fajardo
Otto Fajardo

Reputation: 3407

You can use pyreadstat:

import pyreadstat

pyreadstat.write_sav(mc_all, savFileName)

More information here:

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

Upvotes: 7

Manu
Manu

Reputation: 178

.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)

Upvotes: 2

Related Questions