Python and Sqlite3, transforming data text file to sql database - python

So I've been given an assignment/Challenge to complete but I just don't know whee to start with it I've got experience with Python but not with using databases and data transformation onto the description.
So here is a snippet of my text file I've been given:
Grid-ref= 1, 148
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
Grid-ref= 1, 311
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
Grid-ref= 1, 312
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
So from this i must then create a database containing 4 columns like so:
Xref Yref Date Value
1 148 1,1,2000 3020
1 148 1,2,2000 2820
I hope you can see the pattern so grid-ref= 1, 148 are my X & Y co-ords then each value is obviously the value but i need to iterate through and for each value it then gives it the new date which is just the 1st of each month for 10 years.
So far I have this code which i know isn't much it's a start.
import os
import csv
import sqlite3
f_path = os.path.dirname(os.path.abspath(__file__)) + "/data/"
db = sqlite3.connect('output.db')
cursor = db.cursor()
cursor.execute('CREATE TABLE Data (Xref, Yref, Date, Value)')
date = 2000 - 2010
grid = 'Xref, Yref'
with open(f_path + "data.to.use.txt") as file_read:
for row in csv.DictReader(file_read):
cursor.execute('''INSERT INTO Data
VALUES (:Xref, :Yref, :Date, :Value)''', row)
db.commit()
db.close()
Thank you for all feedback and guidance, I'm in unfamiliar territory with this type of task and hope you can help.

you could try the below code. I am not quite not clear with date requirement . So I just added a month for each entry
from datetime import date,datetime
from dateutil.relativedelta import relativedelta
Xref=''
Yref=''
date =datetime.strptime('2000-01-01', '%Y-%m-%d')
with open('C:\Users\shmathew\Desktop\Sample\sample.txt') as file_read:
for row in file_read:
print row
if 'Grid-ref' in row:
Xref = row.split(',')[0].split('= ')[1]
Yref = row.split(',')[1]
else:
for Value in row.split(' '):
date = date + relativedelta(months=+1)
print Xref.strip(),Yref.strip(),date,Value.strip()
Sample output
490 290 280 230 200 250 440 530 460 420 530 450
1 311 2009-08-01 00:00:00 490
1 311 2009-09-01 00:00:00 290
1 311 2009-10-01 00:00:00 280
1 311 2009-11-01 00:00:00 230
1 311 2009-12-01 00:00:00 200
1 311 2010-01-01 00:00:00 250
1 311 2010-02-01 00:00:00 440
1 311 2010-03-01 00:00:00 530
1 311 2010-04-01 00:00:00 460
1 311 2010-05-01 00:00:00 420
1 311 2010-06-01 00:00:00 530
1 311 2010-07-01 00:00:00 450
490 290 280 230 200 250 440 530 460 420 530 450
1 311 2010-08-01 00:00:00 490
1 311 2010-09-01 00:00:00 290
1 311 2010-10-01 00:00:00 280
1 311 2010-11-01 00:00:00 230
1 311 2010-12-01 00:00:00 200
1 311 2011-01-01 00:00:00 250
1 311 2011-02-01 00:00:00 440
1 311 2011-03-01 00:00:00 530
1 311 2011-04-01 00:00:00 460
1 311 2011-05-01 00:00:00 420
1 311 2011-06-01 00:00:00 530
1 311 2011-07-01 00:00:00 450
490 290 280 230 200 250 440 530 460 420 530 450
1 311 2011-08-01 00:00:00 490
1 311 2011-09-01 00:00:00 290
1 311 2011-10-01 00:00:00 280
1 311 2011-11-01 00:00:00 230
1 311 2011-12-01 00:00:00 200
1 311 2012-01-01 00:00:00 250
1 311 2012-02-01 00:00:00 440
1 311 2012-03-01 00:00:00 530
1 311 2012-04-01 00:00:00 460
1 311 2012-05-01 00:00:00 420
1 311 2012-06-01 00:00:00 530
1 311 2012-07-01 00:00:00 450

Related

ValueError: Invalid classes inferred from unique values of `y` in XGBoost

I'm new to the Data Science field and I'm trying to apply XGBoost in a table having 5 rows × 46 columns
and my last column is my target column.
import sys
!{sys.executable} -m pip install xgboost
import xgboost as xgb
from sklearn.model_selection import train_test_split
x=df_null_mean.iloc[:,:-1]
y=df_null_mean.iloc[:,-1]
x_cols=x.columns
# Splitting the dataset into the Training set and Test set
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
# Fitting XGBoost to the training data
my_model = xgb.XGBClassifier()
my_model.fit(x_train, y_train)
and the error I'm getting is
ValueError: Invalid classes inferred from unique values of `y`. Expected: [ 0 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 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 302 303 304 305
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
648 649 650 651 652 653], got [ 0 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 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 302 303 304 305
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
540 541 542 544 545 546 547 548 549 550 551 553 554 555 556 557 558 559
560 562 563 564 565 567 568 569 570 572 573 574 576 577 578 579 580 581
582 583 584 585 586 587 588 589 590 591 592 593 595 596 598 599 602 605
606 607 608 609 614 615 617 619 622 626 628 629 630 631 632 638 639 640
642 647 650 659 665 673 674 680 684 685 688 691 703 710 713 714 715 716
717 718 719 727 730 731 763 786 812 850 854 857 862 870 876 878 880 884
889 892 894 898 900 902]
Can anyone help me with the resolution?
import sys
!{sys.executable} -m pip install xgboost
import xgboost as xgb
from sklearn.model_selection import train_test_split
x=df_null_mean.iloc[:,:-1]
y=df_null_mean.iloc[:,-1]
x_cols=x.columns
# Splitting the dataset into the Training set and Test set
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
# Fitting XGBoost to the training data
my_model = xgb.XGBClassifier()
my_model.fit(x_train, y_train)
I think you need to have the class numerotated from 0 to n-1 where n is your number of class.
Try this:
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y_train = le.fit_transform(y_train)

Seaborn Lineplot Confidence Interval not visible for all values [duplicate]

I am using sns.lineplot to show the confidence intervals in a plot.
sns.lineplot(x = threshold, y = mrl_array, err_style = 'band', ci=95)
plt.show()
I'm getting the following plot, which doesn't show the confidence interval:
What's the problem?
There is probably only a single observation per x value.
If there is only one observation per x value, then there is no confidence interval to plot.
Bootstrapping is performed per x value, but there needs to be more than one obsevation for this to take effect.
ci: Size of the confidence interval to draw when aggregating with an estimator. 'sd' means to draw the standard deviation of the data. Setting to None will skip bootstrapping.
Note the following examples from seaborn.lineplot.
This is also the case for sns.relplot with kind='line'.
The question specifies sns.lineplot, but this answer applies to any seaborn plot that displays a confidence interval, such as seaborn.barplot.
Data
import seaborn as sns
# load data
flights = sns.load_dataset("flights")
year month passengers
0 1949 Jan 112
1 1949 Feb 118
2 1949 Mar 132
3 1949 Apr 129
4 1949 May 121
# only May flights
may_flights = flights.query("month == 'May'")
year month passengers
4 1949 May 121
16 1950 May 125
28 1951 May 172
40 1952 May 183
52 1953 May 229
64 1954 May 234
76 1955 May 270
88 1956 May 318
100 1957 May 355
112 1958 May 363
124 1959 May 420
136 1960 May 472
# standard deviation for each year of May data
may_flights.set_index('year')[['passengers']].std(axis=1)
year
1949 NaN
1950 NaN
1951 NaN
1952 NaN
1953 NaN
1954 NaN
1955 NaN
1956 NaN
1957 NaN
1958 NaN
1959 NaN
1960 NaN
dtype: float64
# flight in wide format
flights_wide = flights.pivot("year", "month", "passengers")
month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
year
1949 112 118 132 129 121 135 148 148 136 119 104 118
1950 115 126 141 135 125 149 170 170 158 133 114 140
1951 145 150 178 163 172 178 199 199 184 162 146 166
1952 171 180 193 181 183 218 230 242 209 191 172 194
1953 196 196 236 235 229 243 264 272 237 211 180 201
1954 204 188 235 227 234 264 302 293 259 229 203 229
1955 242 233 267 269 270 315 364 347 312 274 237 278
1956 284 277 317 313 318 374 413 405 355 306 271 306
1957 315 301 356 348 355 422 465 467 404 347 305 336
1958 340 318 362 348 363 435 491 505 404 359 310 337
1959 360 342 406 396 420 472 548 559 463 407 362 405
1960 417 391 419 461 472 535 622 606 508 461 390 432
# standard deviation for each year
flights_wide.std(axis=1)
year
1949 13.720147
1950 19.070841
1951 18.438267
1952 22.966379
1953 28.466887
1954 34.924486
1955 42.140458
1956 47.861780
1957 57.890898
1958 64.530472
1959 69.830097
1960 77.737125
dtype: float64
Plots
may_flights has one observation per year, so no CI is shown.
sns.lineplot(data=may_flights, x="year", y="passengers")
sns.barplot(data=may_flights, x='year', y='passengers')
flights_wide shows there are twelve observations for each year, so the CI shows when all of flights is plotted.
sns.lineplot(data=flights, x="year", y="passengers")
sns.barplot(data=flights, x='year', y='passengers')

How do I get the timeseries data plotting in matplotlib in certain timeframe(like 9 am-3pm)?

I have daily timeseries data from 9am to 3pm but, when I am going plot these data in matplotlib, it is taking 24 hours as a day, but I want it take 9AM TO 3PM as a day . How do I get continuous daily graph for only 9 AM TO 3 PM?
This is what I got from my code.
Here is my sample data. I would like to have time-date v/s close plot without any data gap. Please help me.
Pardon me for any mistake here!
close lot1 lot2 time-date
0 800.0 25 50 2020-09-15 11:01:00
1 900.0 25 50 2020-09-15 14:33:00
2 885.85 50 75 2020-09-16 11:45:00
3 791.4 50 125 2020-09-16 12:50:00
4 1082.45 50 75 2020-09-16 14:30:00
5 1060.1 25 125 2020-09-16 14:35:00
6 855.1 50 100 2020-09-17 11:36:00
7 830.0 250 125 2020-09-17 11:39:00
8 815.0 25 125 2020-09-17 11:40:00
9 804.8 25 400 2020-09-17 11:41:00
10 803.0 275 400 2020-09-17 11:44:00
11 791.0 150 650 2020-09-17 11:54:00
12 791.0 100 650 2020-09-17 11:55:00
13 780.65 25 900 2020-09-17 11:59:00
14 784.8 25 925 2020-09-17 12:01:00
15 825.0 25 925 2020-09-17 12:16:00
16 812.3 25 925 2020-09-17 13:25:00
17 816.7 25 950 2020-09-17 14:23:00
18 811.0 25 950 2020-09-17 14:48:00
19 764.5 25 975 2020-09-17 15:11:00
20 808.95 100 1000 2020-09-17 15:20:00
21 805.85 50 1100 2020-09-17 15:24:00
22 798.85 25 1125 2020-09-17 15:27:00
23 812.45 25 1150 2020-09-18 09:17:00
24 814.9 50 1225 2020-09-18 09:18:00
25 840.95 25 1225 2020-09-18 09:19:00
26 839.0 25 1225 2020-09-18 09:20:00
27 827.1 25 1175 2020-09-18 09:23:00
28 812.0 100 1150 2020-09-18 09:28:00
29 770.0 100 1200 2020-09-18 09:32:00
30 784.95 25 1200 2020-09-18 09:33:00
31 790.0 25 1325 2020-09-18 09:35:00
32 788.7 25 1325 2020-09-18 09:37:00
33 789.25 75 1375 2020-09-18 09:38:00
34 810.95 25 1375 2020-09-18 09:42:00
35 827.3 25 1375 2020-09-18 09:43:00
36 821.25 25 1400 2020-09-18 09:45:00
37 809.45 25 1375 2020-09-18 09:57:00
38 820.6 50 1400 2020-09-18 10:01:00
39 835.15 50 1425 2020-09-18 10:04:00
40 832.9 100 1425 2020-09-18 10:05:00
41 839.5 25 1425 2020-09-18 10:07:00
42 831.85 25 1475 2020-09-18 10:09:00
43 808.0 50 1400 2020-09-18 10:14:00
44 795.0 25 1400 2020-09-18 10:17:00
45 780.0 50 1350 2020-09-18 10:26:00
46 802.7 100 1350 2020-09-18 10:28:00
47 792.5 50 1425 2020-09-18 10:29:00
48 790.7 75 1425 2020-09-18 10:30:00
49 793.0 25 1425 2020-09-18 10:34:00
50 789.65 25 1425 2020-09-18 10:35:00
51 796.9 50 1425 2020-09-18 10:37:00
52 791.5 25 1425 2020-09-18 10:38:00
53 797.1 50 1475 2020-09-18 10:39:00
54 760.0 50 1475 2020-09-18 10:41:00
55 780.65 100 1475 2020-09-18 10:42:00
56 782.4 50 1475 2020-09-18 10:43:00
57 780.0 100 1550 2020-09-18 10:45:00
58 788.6 75 1650 2020-09-18 10:50:00
59 794.75 25 1650 2020-09-18 10:53:00
60 792.8 150 1650 2020-09-18 10:54:00
61 806.5 150 1650 2020-09-18 10:55:00
62 801.0 50 1775 2020-09-18 10:57:00
63 789.4 50 1775 2020-09-18 10:58:00
64 804.55 25 1775 2020-09-18 11:00:00
65 792.0 25 1775 2020-09-18 11:03:00
66 793.9 50 1775 2020-09-18 11:05:00
67 785.1 225 1850 2020-09-18 11:06:00
68 782.45 50 1850 2020-09-18 11:07:00
69 778.05 50 1850 2020-09-18 11:08:00
70 766.2 175 2000 2020-09-18 11:12:00
71 772.1 75 2000 2020-09-18 11:13:00
72 758.9 100 2200 2020-09-18 11:14:00
73 771.0 250 2200 2020-09-18 11:15:00
74 764.7 25 2200 2020-09-18 11:16:00
75 777.45 125 2450 2020-09-18 11:19:00
76 778.2 25 2550 2020-09-18 11:22:00
77 777.85 25 2550 2020-09-18 11:23:00
78 783.85 125 2600 2020-09-18 11:24:00
79 776.8 100 2700 2020-09-18 11:26:00
80 776.2 75 2700 2020-09-18 11:28:00
81 785.8 75 2875 2020-09-18 11:30:00
82 787.45 100 2875 2020-09-18 11:31:00
83 789.9 25 2875 2020-09-18 11:32:00
84 798.1 75 2875 2020-09-18 11:33:00
85 792.85 50 2925 2020-09-18 11:38:00
86 794.2 100 2925 2020-09-18 11:40:00
87 796.55 25 3050 2020-09-18 11:42:00
88 800.0 25 3050 2020-09-18 11:44:00
89 781.9 525 3050 2020-09-18 11:50:00
90 787.85 50 3525 2020-09-18 11:51:00
91 787.15 350 3525 2020-09-18 11:53:00
92 789.0 25 3525 2020-09-18 11:54:00
93 801.9 50 3375 2020-09-18 11:57:00
94 793.3 25 3400 2020-09-18 12:02:00
95 793.4 25 3000 2020-09-18 12:07:00
96 795.0 25 3000 2020-09-18 12:08:00
97 800.95 25 3025 2020-09-18 12:09:00
98 800.9 75 3025 2020-09-18 12:10:00
99 792.85 25 3025 2020-09-18 12:11:00
100 785.3 75 3075 2020-09-18 12:12:00
101 790.8 50 3075 2020-09-18 12:13:00
102 782.0 125 3075 2020-09-18 12:14:00
103 791.9 25 3125 2020-09-18 12:15:00
104 789.9 25 3125 2020-09-18 12:16:00
105 799.65 125 3150 2020-09-18 12:18:00
106 795.15 50 3025 2020-09-18 12:30:00
107 794.05 25 3075 2020-09-18 12:36:00
108 785.75 25 3075 2020-09-18 12:37:00
109 758.5 25 3100 2020-09-18 12:45:00
110 775.0 50 3100 2020-09-18 12:46:00
111 771.6 100 3100 2020-09-18 12:47:00
112 768.6 25 3125 2020-09-18 12:48:00
113 786.95 50 3150 2020-09-18 12:53:00
114 781.5 25 3150 2020-09-18 12:54:00
115 774.95 25 3100 2020-09-18 12:58:00
116 771.7 25 3100 2020-09-18 12:59:00
117 766.7 50 3150 2020-09-18 13:00:00
118 764.55 125 3150 2020-09-18 13:01:00
119 767.0 200 3150 2020-09-18 13:02:00
120 770.0 250 3175 2020-09-18 13:05:00
121 770.15 75 3075 2020-09-18 13:06:00
122 789.15 50 3075 2020-09-18 13:08:00
123 777.7 50 3125 2020-09-18 13:10:00
124 787.0 50 3125 2020-09-18 13:11:00
125 790.0 100 3000 2020-09-18 13:14:00
126 795.0 275 3000 2020-09-18 13:15:00
127 775.0 25 3000 2020-09-18 13:20:00
128 780.0 75 2900 2020-09-18 13:21:00
129 774.15 75 2900 2020-09-18 13:22:00
130 769.0 100 2825 2020-09-18 13:24:00
131 753.2 175 2825 2020-09-18 13:25:00
132 761.7 25 2825 2020-09-18 13:26:00
133 775.0 75 2975 2020-09-18 13:30:00
134 766.15 650 3000 2020-09-18 13:37:00
135 767.5 25 3375 2020-09-18 13:40:00
136 778.0 25 3375 2020-09-18 13:42:00
137 782.0 25 3375 2020-09-18 13:43:00
138 776.5 25 3375 2020-09-18 13:44:00
139 796.1 75 3375 2020-09-18 13:45:00
140 795.2 25 3175 2020-09-18 13:48:00
141 802.55 175 3175 2020-09-18 13:49:00
142 806.3 100 3175 2020-09-18 13:50:00
143 806.65 125 3450 2020-09-18 13:51:00
144 788.2 50 3450 2020-09-18 13:52:00
145 796.25 50 3600 2020-09-18 13:55:00
146 795.0 25 3600 2020-09-18 13:56:00
147 784.4 25 3250 2020-09-18 14:01:00
148 790.0 25 3175 2020-09-18 14:05:00
149 790.0 25 3200 2020-09-18 14:06:00
150 780.0 25 3200 2020-09-18 14:07:00
151 775.0 225 3200 2020-09-18 14:08:00
152 779.0 100 3450 2020-09-18 14:10:00
153 767.0 100 3500 2020-09-18 14:12:00
154 769.0 25 3500 2020-09-18 14:13:00
155 759.7 25 3625 2020-09-18 14:17:00
156 764.45 100 3650 2020-09-18 14:18:00
157 750.0 25 3650 2020-09-18 14:19:00
158 701.9 525 3650 2020-09-18 14:20:00
159 631.0 1725 4050 2020-09-18 14:21:00
160 600.0 275 4050 2020-09-18 14:22:00
161 643.5 500 4050 2020-09-18 14:23:00
162 606.9 475 4275 2020-09-18 14:24:00
163 599.0 1000 4275 2020-09-18 14:25:00
164 590.45 675 4275 2020-09-18 14:26:00
165 605.0 925 4950 2020-09-18 14:27:00
166 614.3 600 4950 2020-09-18 14:28:00
167 600.05 775 4950 2020-09-18 14:29:00
168 595.35 1150 7025 2020-09-18 14:30:00
169 596.2 525 7025 2020-09-18 14:31:00
170 596.8 975 7025 2020-09-18 14:32:00
171 584.0 200 8125 2020-09-18 14:33:00
172 550.0 1750 8125 2020-09-18 14:34:00
173 552.2 1825 8125 2020-09-18 14:35:00
174 554.65 600 8750 2020-09-18 14:36:00
175 565.4 925 8750 2020-09-18 14:37:00
176 565.0 150 8750 2020-09-18 14:38:00
177 583.95 450 9150 2020-09-18 14:39:00
178 561.4 975 9150 2020-09-18 14:40:00
179 566.8 3450 9150 2020-09-18 14:41:00
180 563.35 425 10525 2020-09-18 14:42:00
181 565.4 700 10525 2020-09-18 14:43:00
182 570.0 650 10525 2020-09-18 14:44:00
183 572.8 200 11125 2020-09-18 14:45:00
184 595.25 750 11125 2020-09-18 14:46:00
185 585.75 625 11125 2020-09-18 14:47:00
186 593.4 475 10925 2020-09-18 14:48:00
187 590.0 950 10925 2020-09-18 14:49:00
188 576.4 775 10925 2020-09-18 14:50:00
189 596.55 775 10800 2020-09-18 14:51:00
190 595.95 475 10800 2020-09-18 14:52:00
191 593.95 725 10800 2020-09-18 14:53:00
192 611.45 1500 10550 2020-09-18 14:54:00
193 618.0 1050 10550 2020-09-18 14:55:00
194 600.0 1150 10550 2020-09-18 14:56:00
195 609.15 575 11025 2020-09-18 14:57:00
196 615.6 375 11025 2020-09-18 14:58:00
197 604.4 875 11025 2020-09-18 14:59:00
198 591.3 1225 11375 2020-09-18 15:00:00
199 600.0 1100 11375 2020-09-18 15:01:00
200 597.8 1800 11375 2020-09-18 15:02:00
201 605.0 550 12625 2020-09-18 15:03:00
202 599.1 325 12625 2020-09-18 15:04:00
203 606.2 500 12625 2020-09-18 15:05:00
204 614.65 850 12625 2020-09-18 15:06:00
205 616.0 1225 12625 2020-09-18 15:07:00
206 622.5 1325 12625 2020-09-18 15:08:00
207 620.0 750 13850 2020-09-18 15:09:00
208 632.0 525 13850 2020-09-18 15:10:00
209 630.3 375 13850 2020-09-18 15:11:00
210 635.0 425 13575 2020-09-18 15:12:00
211 630.85 400 13575 2020-09-18 15:13:00
212 627.45 275 13575 2020-09-18 15:14:00
213 620.7 200 13700 2020-09-18 15:15:00
214 622.4 200 13700 2020-09-18 15:16:00
215 631.6 625 13700 2020-09-18 15:17:00
216 624.95 525 13100 2020-09-18 15:18:00
217 632.25 775 13100 2020-09-18 15:19:00
218 610.7 350 13100 2020-09-18 15:20:00
219 602.0 575 13175 2020-09-18 15:21:00
220 612.4 200 13175 2020-09-18 15:22:00
221 617.25 325 13175 2020-09-18 15:23:00
222 617.8 300 13650 2020-09-18 15:24:00
223 622.1 600 13650 2020-09-18 15:25:00
224 622.2 250 13650 2020-09-18 15:26:00
225 623.7 300 13425 2020-09-18 15:27:00
226 622.5 425 13425 2020-09-18 15:28:00
227 621.0 375 13425 2020-09-18 15:29:00
228 567.55 1075 13275 2020-09-21 09:15:00
229 565.0 2100 13275 2020-09-21 09:16:00
230 560.0 1100 14925 2020-09-21 09:17:00
231 562.15 625 14925 2020-09-21 09:18:00
232 556.45 850 14925 2020-09-21 09:19:00
233 543.1 1525 16450 2020-09-21 09:20:00
234 538.0 800 16450 2020-09-21 09:21:00
235 537.45 575 16450 2020-09-21 09:22:00
236 544.4 775 16825 2020-09-21 09:23:00
237 545.7 500 16825 2020-09-21 09:24:00
238 551.4 550 16825 2020-09-21 09:25:00
239 544.25 900 17625 2020-09-21 09:26:00
240 538.0 1850 17625 2020-09-21 09:27:00
241 534.85 525 17625 2020-09-21 09:28:00
242 534.5 425 18775 2020-09-21 09:29:00
243 547.1 1075 18775 2020-09-21 09:30:00
244 536.85 375 18775 2020-09-21 09:31:00
245 547.6 375 19775 2020-09-21 09:32:00
246 540.25 350 19775 2020-09-21 09:33:00
247 541.2 375 19775 2020-09-21 09:34:00
248 544.6 175 18650 2020-09-21 09:35:00
249 542.55 250 18650 2020-09-21 09:36:00
250 539.65 550 18650 2020-09-21 09:37:00
251 531.15 2150 19175 2020-09-21 09:38:00
252 530.1 825 19175 2020-09-21 09:39:00
253 518.7 1575 19175 2020-09-21 09:40:00
254 520.95 575 20475 2020-09-21 09:41:00
255 511.45 1250 20475 2020-09-21 09:42:00
256 517.45 1025 20475 2020-09-21 09:43:00
257 515.0 550 21150 2020-09-21 09:44:00
258 515.0 1125 21150 2020-09-21 09:45:00
259 518.9 425 21150 2020-09-21 09:46:00
260 514.95 550 21500 2020-09-21 09:47:00
261 509.8 1625 21500 2020-09-21 09:48:00
262 507.55 475 21500 2020-09-21 09:49:00
263 514.35 500 21975 2020-09-21 09:50:00
264 524.5 500 21975 2020-09-21 09:51:00
265 527.45 550 21975 2020-09-21 09:52:00
266 527.3 675 21550 2020-09-21 09:53:00
267 521.3 525 21550 2020-09-21 09:54:00
268 520.0 275 21550 2020-09-21 09:55:00
269 519.5 750 21600 2020-09-21 09:56:00
270 516.7 400 21600 2020-09-21 09:57:00
271 517.75 350 21600 2020-09-21 09:58:00
272 511.9 575 21850 2020-09-21 09:59:00
273 507.9 1175 21850 2020-09-21 10:00:00
274 510.05 525 21850 2020-09-21 10:01:00
275 515.85 1025 22350 2020-09-21 10:02:00
276 514.25 600 22350 2020-09-21 10:03:00
277 520.05 650 22350 2020-09-21 10:04:00
278 518.9 950 22850 2020-09-21 10:05:00
279 512.25 550 22850 2020-09-21 10:06:00
280 513.65 650 22850 2020-09-21 10:07:00
281 514.0 525 24300 2020-09-21 10:08:00
282 506.3 875 24300 2020-09-21 10:09:00
283 490.85 1825 24300 2020-09-21 10:10:00
284 499.0 300 25050 2020-09-21 10:11:00
285 495.0 975 25050 2020-09-21 10:12:00
286 497.15 1125 25050 2020-09-21 10:13:00
287 496.8 625 24875 2020-09-21 10:14:00
288 492.95 1075 24875 2020-09-21 10:15:00
289 497.6 1125 24875 2020-09-21 10:16:00
290 502.3 775 24450 2020-09-21 10:17:00
291 501.85 475 24450 2020-09-21 10:18:00
292 502.85 800 24450 2020-09-21 10:19:00
293 511.35 825 24700 2020-09-21 10:20:00
294 537.0 1850 24700 2020-09-21 10:21:00
295 531.55 2025 24700 2020-09-21 10:22:00
296 558.45 2825 24675 2020-09-21 10:23:00
297 555.25 625 24675 2020-09-21 10:24:00
298 577.0 4275 24675 2020-09-21 10:25:00
299 574.0 1075 23500 2020-09-21 10:26:00
300 569.4 600 23500 2020-09-21 10:27:00

read csv and Iterate through 10 row blocks

I am trying to read a CSV file and Iterate through 10-row blocks.
The data is quite unusual, with two columns and 10-row blocks.
57485 rows x 2 columns in the format below:
Grid-ref= 1,148
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
Grid-ref= 1,311
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
Grid-ref= 1,312
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
Every 10 rows consist of a grid reference and two records X/Y ref.
The grid reference and X value is in column 1, the Y value is in column 2, and then 9 rows with 12 columns, in column one.
The blocks (x) 0 - 9, represent months (Jan - December
The blocks (y) 0 - 9, represent years (1991-2000)
So for 0, is 1991
3020 is January 1991, 2820 is February 1991
Grid-ref = 1 (X),148 (Y)
The code below reads 10 rows, but keeps repeating the first row in all following 10-row blocks??
I don't understand why it keeps repeating the first row??
Any suggestions to resolve this would be appreciated..
## Python 3.6
## Read in the datasets (they are in CSV format)
data = pd.read_csv('cru-ts-2-10-1991-2000-cutdown.csv', skiprows=5, na_values = [-999] )
## View data >> 57485 rows x 2 columns
#print(data)
#print(len(data)) ## len = 57485
## header = pd.MultiIndex.from_product([['Grid-ref', 'Xref', 'Yref'], ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] ])
# df = pd.DataFrame(np.random.randn(10, 11),
# index=['1991','1992','1993','1994','1995', '1996', '1997', '1998', '1999', '2000', '2001'],
# columns=header)
# print(data.head(10)) ## prints chunks of 10 rows
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
for group in chunker(data, 10):
print(group)
The first two block:
Grid-ref= 1 148
0 3020 2820 3040 2880 1740 1360 980 990 1410 ... NaN
1 3020 2820 3040 2880 1740 1360 980 990 1410 ... NaN
2 3020 2820 3040 2880 1740 1360 980 990 1410 ... NaN
3 3020 2820 3040 2880 1740 1360 980 990 1410 ... NaN
4 3020 2820 3040 2880 1740 1360 980 990 1410 ... NaN
5 3020 2820 3040 2880 1740 1360 980 990 1410 ... NaN
6 3020 2820 3040 2880 1740 1360 980 990 1410 ... NaN
7 3020 2820 3040 2880 1740 1360 980 990 1410 ... NaN
8 3020 2820 3040 2880 1740 1360 980 990 1410 ... NaN
9 3020 2820 3040 2880 1740 1360 980 990 1410 ... NaN
Grid-ref= 1 148
10 Grid-ref= 1 311.0
11 490 290 280 230 200 250 440 530 460 ... NaN
12 490 290 280 230 200 250 440 530 460 ... NaN
13 490 290 280 230 200 250 440 530 460 ... NaN
14 490 290 280 230 200 250 440 530 460 ... NaN
15 490 290 280 230 200 250 440 530 460 ... NaN
16 490 290 280 230 200 250 440 530 460 ... NaN
17 490 290 280 230 200 250 440 530 460 ... NaN
18 490 290 280 230 200 250 440 530 460 ... NaN
19 490 290 280 230 200 250 440 530 460 ... NaN
Pandas is good for uniform columnar data. If your input isn't uniform, you can preprocess it and then load the dataframe. This one is easy, all you need to do is scan for grid headers and remove them. Since the data itself is numeric, separated by whitespace, a simple split will parse it. This example creates a list but if the dataset is large, it may be reasonable to write to an intermediate file instead.
import csv
import re
import pandas as pd
grid_re = re.compile(r"Grid-ref=\s*(\d+),(\d+)")
with open('test.csv') as fobj:
table = []
try:
while True:
# find next block
for line in fobj:
match = grid_re.search(line)
if match:
grid_xy = list(match.groups())
break
else:
raise StopIteration()
for _ in range(10):
line = next(fobj)
# add row plus grid columns
table.append(line.strip().split() + grid_xy)
except StopIteration:
pass
df = pd.DataFrame(table)
print(df)

Calculating mean values in dataframe and adding to new columns

I need to calculate average values (row wise without index) of columns with constant step.
I have already done a simple operation for the first 4 columns. It works nicely. After that I have created a list with column names (for storing average values) for dataframe. I have found out that I can do this using apply and lambda. I have tried many variants to get a result, but I have not found a solution.
data= np.arange(400).reshape(20,20)
df=pd.DataFrame(data=data)
df.columns=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T']
df['A1_avg'] = df[['A', 'B', 'C', 'D']].mean(axis=1)
colnames_avg=['A1_avg', 'A2_avg', 'A3_avg', 'A4_avg', 'A5_avg']
df.head()
I have tried this code for generating 5 extra columns containing the average of several subsets of data:
df[colnames_avg]=df[colnames_avg].applymap(lambda x: df[['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H'], ['I', 'J', 'K', 'L'],['M', 'N', 'O', 'P'],['Q', 'R', 'S', 'T']].mean(axis=1)
Is it possible to do this with the range function with a predefined step (e.g. 4)?
I would do that as follows in a loop, going over the columns and cutting them into groups of 4 columns each (the last group might be smaller):
cols=list(df.columns)
while len(cols) > 0:
group= cols[:4]
cols= cols[4:]
df['mean_' + '_'.join(group)]= df[group].mean(axis='columns')
The result looks like
df[[col for col in df if col.startswith('mean_')]]
mean_A_B_C_D mean_E_F_G_H mean_I_J_K_L mean_M_N_O_P mean_Q_R_S_T
0 1.5 5.5 9.5 13.5 17.5
1 21.5 25.5 29.5 33.5 37.5
2 41.5 45.5 49.5 53.5 57.5
3 61.5 65.5 69.5 73.5 77.5
4 81.5 85.5 89.5 93.5 97.5
5 101.5 105.5 109.5 113.5 117.5
...
If you want result columns like A1..., just add a counter variable in the loop and use 'A{}'.format(i) as the column name.
Method 1: numpy.split & DataFrame.loc:
We can split your columns into evenly size chunks and then use .loc to create the new columns:
for idx, chunk in enumerate(np.split(df.columns, len(df.columns)/4)):
df[f'A{idx+1}_avg'] = df.loc[:, chunk].mean(axis=1)
Output
A B C D E F G H I J ... P Q R S T A1_avg A2_avg A3_avg A4_avg A5_avg
0 0 1 2 3 4 5 6 7 8 9 ... 15 16 17 18 19 1.5 5.5 9.5 13.5 17.5
1 20 21 22 23 24 25 26 27 28 29 ... 35 36 37 38 39 21.5 25.5 29.5 33.5 37.5
2 40 41 42 43 44 45 46 47 48 49 ... 55 56 57 58 59 41.5 45.5 49.5 53.5 57.5
3 60 61 62 63 64 65 66 67 68 69 ... 75 76 77 78 79 61.5 65.5 69.5 73.5 77.5
4 80 81 82 83 84 85 86 87 88 89 ... 95 96 97 98 99 81.5 85.5 89.5 93.5 97.5
5 100 101 102 103 104 105 106 107 108 109 ... 115 116 117 118 119 101.5 105.5 109.5 113.5 117.5
6 120 121 122 123 124 125 126 127 128 129 ... 135 136 137 138 139 121.5 125.5 129.5 133.5 137.5
7 140 141 142 143 144 145 146 147 148 149 ... 155 156 157 158 159 141.5 145.5 149.5 153.5 157.5
8 160 161 162 163 164 165 166 167 168 169 ... 175 176 177 178 179 161.5 165.5 169.5 173.5 177.5
9 180 181 182 183 184 185 186 187 188 189 ... 195 196 197 198 199 181.5 185.5 189.5 193.5 197.5
10 200 201 202 203 204 205 206 207 208 209 ... 215 216 217 218 219 201.5 205.5 209.5 213.5 217.5
11 220 221 222 223 224 225 226 227 228 229 ... 235 236 237 238 239 221.5 225.5 229.5 233.5 237.5
12 240 241 242 243 244 245 246 247 248 249 ... 255 256 257 258 259 241.5 245.5 249.5 253.5 257.5
13 260 261 262 263 264 265 266 267 268 269 ... 275 276 277 278 279 261.5 265.5 269.5 273.5 277.5
14 280 281 282 283 284 285 286 287 288 289 ... 295 296 297 298 299 281.5 285.5 289.5 293.5 297.5
15 300 301 302 303 304 305 306 307 308 309 ... 315 316 317 318 319 301.5 305.5 309.5 313.5 317.5
16 320 321 322 323 324 325 326 327 328 329 ... 335 336 337 338 339 321.5 325.5 329.5 333.5 337.5
17 340 341 342 343 344 345 346 347 348 349 ... 355 356 357 358 359 341.5 345.5 349.5 353.5 357.5
18 360 361 362 363 364 365 366 367 368 369 ... 375 376 377 378 379 361.5 365.5 369.5 373.5 377.5
19 380 381 382 383 384 385 386 387 388 389 ... 395 396 397 398 399 381.5 385.5 389.5 393.5 397.5
Method 2: .range & iloc:
We can create a range for each 4 columns, then use iloc to acces each slice of your dataframe and calculate the mean and at the same time create your new column:
slices = range(0, len(df.columns)+1, 4)
for idx, rng in enumerate(slices):
if idx > 0:
df[f'A{idx}_avg'] = df.iloc[:, slices[idx-1]:slices[idx]].mean(axis=1)
Output
A B C D E F G H I J ... P Q R S T A1_avg A2_avg A3_avg A4_avg A5_avg
0 0 1 2 3 4 5 6 7 8 9 ... 15 16 17 18 19 1.5 5.5 9.5 13.5 17.5
1 20 21 22 23 24 25 26 27 28 29 ... 35 36 37 38 39 21.5 25.5 29.5 33.5 37.5
2 40 41 42 43 44 45 46 47 48 49 ... 55 56 57 58 59 41.5 45.5 49.5 53.5 57.5
3 60 61 62 63 64 65 66 67 68 69 ... 75 76 77 78 79 61.5 65.5 69.5 73.5 77.5
4 80 81 82 83 84 85 86 87 88 89 ... 95 96 97 98 99 81.5 85.5 89.5 93.5 97.5
5 100 101 102 103 104 105 106 107 108 109 ... 115 116 117 118 119 101.5 105.5 109.5 113.5 117.5
6 120 121 122 123 124 125 126 127 128 129 ... 135 136 137 138 139 121.5 125.5 129.5 133.5 137.5
7 140 141 142 143 144 145 146 147 148 149 ... 155 156 157 158 159 141.5 145.5 149.5 153.5 157.5
8 160 161 162 163 164 165 166 167 168 169 ... 175 176 177 178 179 161.5 165.5 169.5 173.5 177.5
9 180 181 182 183 184 185 186 187 188 189 ... 195 196 197 198 199 181.5 185.5 189.5 193.5 197.5
10 200 201 202 203 204 205 206 207 208 209 ... 215 216 217 218 219 201.5 205.5 209.5 213.5 217.5
11 220 221 222 223 224 225 226 227 228 229 ... 235 236 237 238 239 221.5 225.5 229.5 233.5 237.5
12 240 241 242 243 244 245 246 247 248 249 ... 255 256 257 258 259 241.5 245.5 249.5 253.5 257.5
13 260 261 262 263 264 265 266 267 268 269 ... 275 276 277 278 279 261.5 265.5 269.5 273.5 277.5
14 280 281 282 283 284 285 286 287 288 289 ... 295 296 297 298 299 281.5 285.5 289.5 293.5 297.5
15 300 301 302 303 304 305 306 307 308 309 ... 315 316 317 318 319 301.5 305.5 309.5 313.5 317.5
16 320 321 322 323 324 325 326 327 328 329 ... 335 336 337 338 339 321.5 325.5 329.5 333.5 337.5
17 340 341 342 343 344 345 346 347 348 349 ... 355 356 357 358 359 341.5 345.5 349.5 353.5 357.5
18 360 361 362 363 364 365 366 367 368 369 ... 375 376 377 378 379 361.5 365.5 369.5 373.5 377.5
19 380 381 382 383 384 385 386 387 388 389 ... 395 396 397 398 399 381.5 385.5 389.5 393.5 397.5
[20 rows x 25 columns]

Categories

Resources