I have a list of file
JavaScript
x
2
1
file_names=['file1.sav', 'file2.sav']
2
and a list of variables names
JavaScript
1
2
1
var_names=['var1', 'var2']
2
I want to assign every item in var_names
a read_spss
function.
so that ill get
JavaScript
1
3
1
var1= pd.read_spss('file1.sav')
2
var2= pd.read_spss('file2.sav')
3
Thanks
Advertisement
Answer
Like the comment above the easiest way to do this is with a dictionary.
JavaScript
1
10
10
1
#create empty dictionary
2
files = {}
3
4
file_names=['file1.sav', 'file2.sav']
5
var_names=['var1', 'var2']
6
7
#loop over both file_names and var_names and use them to build the dictionary
8
for var_name, file in zip(var_names, file_names):
9
files[var_name] = pd.read_spss(file)
10
That way then you can just access it like this files['var1']
to get the content of file1.sav1
.