-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_livingspace_commutes.py
More file actions
194 lines (139 loc) · 6.24 KB
/
get_livingspace_commutes.py
File metadata and controls
194 lines (139 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 12 12:49:05 2022
tyomatka:
Information only from 2005 onwards
ptoim2:
11 = employed
12 = unemployed
21 = 0-14 yrs old
22 = student
24 = retired
25 = conscript
29 = retired on base of unemployment
99 = outside workforce for other reasons
asva:
1 = spacious. 1-5 person household where the aparment has has at least 3 rooms more than inhabitants.
2 = normal. one person per room.
3 = cramped. more than one person per room.
4 = unknown.
ps. kitchen is not counted as a "room" from 1989 onwards
@author: TuoVaisanen-e01
"""
import pandas as pd
import gc
# file path list
tkt_paths = ["D:\\ready-made\\FOLK_tkt_8800a\\folk_19872000_tua_tkt21tot_1.dta",
"D:\\ready-made\\FOLK_tkt_0110a\\folk_20012010_tua_tkt21tot_1.dta",
"D:\\ready-made\\FOLK_tkt_11a\\folk_20112018_tua_tkt21tot_1.dta",]
ask_paths = ["D:\\ready-made\\FOLK_askun_8700a\\folk_19872000_tua_askun21tot_1.csv",
"D:\\ready-made\\FOLK_askun_0110a\\folk_20012010_tua_askun21tot_1.csv",
"D:\\ready-made\\FOLK_askun_11a\\folk_20112019_tua_askun21tot_1.csv"]
# column list to read, tyomatka only available from 2005
tktcols = ['vuosi', 'shnro', 'tyomatka', 'ptoim2']
askcols = ['vuosi', 'shnro', 'asva']
# loop over paths
for path in tkt_paths:
# list for dataframes
df_list = []
# loop over data and separate annual datasets
for chunk in pd.read_stata(path, chunksize=100000, columns=tktcols):
# check if year changes in chunk
if len(list(chunk['vuosi'].unique())) == 2:
# get years present in dataframe
prevyear = chunk['vuosi'].min()
newyear = chunk['vuosi'].max()
# print indication message
print('[INFO] - Reached end of FOLK data for ' + str(prevyear))
# split dataframe in two based on year
prevdf = chunk[chunk['vuosi'] == prevyear]
newdf = chunk[chunk['vuosi'] == newyear]
# append previous year to df list
df_list.append(prevdf)
# concatenate into single dataframe
data = pd.concat(df_list, ignore_index=True)
# print indication message
print('[INFO] - Saving FOLK tkt data for ' + str(prevyear) + '...')
# save the full annual data
data.to_csv('W:\\FOLK\csv\\FOLK_tkt_data_' + str(prevyear) + '.csv',
sep=',', encoding='utf-8')
# print indication message
print('[INFO] - Started processing FOLK tkt data for ' + str(newyear))
# empty dataframe list for next year data
df_list = []
# add next year data in
df_list.append(newdf)
# release memory
del prevdf
del newdf
gc.collect()
# check if year changes
elif len(list(chunk['vuosi'].unique())) == 1:
# get current year
curyear = chunk['vuosi'].max()
# append to df list
df_list.append(chunk)
# concatenate into single dataframe
data = pd.concat(df_list, ignore_index=True)
# print indication message
print('[INFO] - Saving FOLK tkt data for ' + str(curyear) + 'e...')
# save
data.to_csv('W:\\FOLK\\csv\\FOLK_tkt_data_' + str(curyear) + '.csv',
sep=',', encoding='utf-8')
# release memory
del data
gc.collect()
# loop over paths
for path in ask_paths:
# list for dataframes
df_list = []
# loop over data and separate annual datasets
for chunk in pd.read_csv(path, sep=',', encoding='utf-8', chunksize=100000,
usecols=askcols):
# check if year changes in chunk
if len(list(chunk['vuosi'].unique())) == 2:
# get years present in dataframe
prevyear = chunk['vuosi'].min()
newyear = chunk['vuosi'].max()
# print indication message
print('[INFO] - Reached end of FOLK askun data for ' + str(prevyear))
# split dataframe in two based on year
prevdf = chunk[chunk['vuosi'] == prevyear]
newdf = chunk[chunk['vuosi'] == newyear]
# append previous year to df list
df_list.append(prevdf)
# concatenate into single dataframe
data = pd.concat(df_list, ignore_index=True)
# print indication message
print('[INFO] - Saving FOLK data for ' + str(prevyear) + '...')
# save the full annual data
data.to_csv('W:\\FOLK\csv\\FOLK_askun_data_' + str(prevyear) + '.csv',
sep=',', encoding='utf-8')
# print indication message
print('[INFO] - Started processing FOLK askun data for ' + str(newyear))
# empty dataframe list for next year data
df_list = []
# add next year data in
df_list.append(newdf)
# release memory
del prevdf
del newdf
gc.collect()
# check if year changes
elif len(list(chunk['vuosi'].unique())) == 1:
# get current year
curyear = chunk['vuosi'].max()
# append to df list
df_list.append(chunk)
# concatenate into single dataframe
data = pd.concat(df_list, ignore_index=True)
# print indication message
print('[INFO] - Saving FOLK data for ' + str(curyear) + 'e...')
# save
data.to_csv('W:\\FOLK\\csv\\FOLK_askun_data_' + str(curyear) + '.csv',
sep=',', encoding='utf-8')
# release memory
del data
gc.collect()
# print message
print('[INFO] - ... done!')