preprocess_data(df, variables_of_interest=variables_of_interest, weekday=True, unique_trips_only=False, process_durations=False)
Preprocess the NHTS data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
The input dataframe containing NHTS data. |
required |
variables_of_interest |
dict
|
A dictionary specifying the variables of interest and their types (categorical, numerical, time). |
variables_of_interest
|
weekday |
bool
|
If True, processes data for weekdays; if False, processes data for weekends. Defaults to True. |
True
|
unique_trips_only |
bool
|
If True, removes duplicate trips based on id, start_time, and end_time. Defaults to False. |
False
|
process_durations |
bool
|
If True, processes the durations of activities and travels. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
pd.DataFrame: The preprocessed dataframe. |
Notes
- The function replaces specific values indicating no data with NaN and drops rows with NaN values.
- It filters the dataframe to include only the specified variables of interest.
- The function processes categorical, numerical, and time variables according to their specified types.
- It renames columns to standardize names and drops certain columns not needed for further analysis.
- If
unique_trips_only
is True, duplicate trips are removed. - If
process_durations
is True, the function calculates the travel and activity durations.
Examples:
>>> variables_of_interest = {
... 'Kön': {'type': 'categorical', 'categories': {1: 'Male', 2: 'Female'}},
... 'Åldersgrupp': {'type': 'categorical', 'categories': {1: '0-17', 2: '18-34', 3: '35-64', 4: '65+'}},
... 'Starttid': {'type': 'time'},
... 'Sluttid': {'type': 'time'},
... 'Reslängd': {'type': 'numerical'},
... # additional variables...
... }
>>> df = preprocess_data(df, variables_of_interest, weekday=True, unique_trips_only=True, process_durations=True)
Source code in tripsender\nhts.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
|