Calculating mean values in dataframe and adding to new columns - python

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]

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

Group list of value by range then set the values of the group to a specified one

I have some python pandas dataframe like this one. It lists the position of labels in a printed table extracted with ocr. So each label position have a little offset.
left top text
4 66 23 6/22/2021
6 66 82 6/23/2021
8 65 142 6/24/2021
10 65 202 6/25/2021
12 64 262 6/26/2021
16 345 25 14:00
18 354 85 7:30
20 344 145 13:00
22 344 206 11:00
24 343 265 10:00
26 343 325 15:00
30 859 23 20:30
32 860 84 14:00
34 858 144 20:23
36 859 204 18:00
38 858 264 13:00
40 858 324 18:15
44 1091 23 6:30
46 1091 84 6:30
48 1090 144 7:23
50 1090 204 7:00
52 1089 264 3:00
54 1089 324 3:15
56 1088 383 0:00
58 1087 443 0:00
60 1087 503 0:00
62 1047 563 33:38:00
I need to sort the data by the "left" column value, then set each group of values to a specific value.
In this case, the first five value [66,66,65,65,64] can be grouped together because they are in a narrow range (for instance [60...70]). The first five values will then be set to the min value of the range ([60...70], but it can be the range of the values [64...66]).
And so on, for each group of value, grouped by the fact that their values are in a narrow range.
The size of each group if random. In this case the last row have a "left" value of [1047]. It fits in a single value group.
The values are also random. I can't use this solution as far as I understand it : how to group by list ranges of value in python pandas
I will then do the same work for the second column "top".
What is the trick to do this ?
I know there is a mathematical way to do this. I can use it in some daw to "sharpen" a sound. But maybe there is a python pandas way to do this.
I'm not native english speaker. So I hope you understand me.
Thank you for your time
Edit:
What I want (but it can be the min value of each group, or here the first value of the group):
left top text
4 66 23 6/22/2021
6 66 82 6/23/2021
8 66 142 6/24/2021
10 66 202 6/25/2021
12 66 262 6/26/2021
16 345 25 14:00
18 345 85 7:30
20 345 145 13:00
22 345 206 11:00
24 345 265 10:00
26 345 325 15:00
30 859 23 20:30
32 859 84 14:00
34 859 144 20:23
36 859 204 18:00
38 859 264 13:00
40 859 324 18:15
44 1091 23 6:30
46 1091 84 6:30
48 1091 144 7:23
50 1091 204 7:00
52 1091 264 3:00
54 1091 324 3:15
56 1091 383 0:00
58 1091 443 0:00
60 1091 503 0:00
62 1047 563 33:38:00
One way using pandas.Series.diff with cumsum trick to groupby.
Then use pandas.DataFrame.groupby.transform with "min":
df = df.sort_values("left")
ind1 = df["left"].diff().fillna(0).gt(10).cumsum()
df["left_min"] = df.groupby(ind1)["left"].transform("min")
df = df.sort_values("top")
ind2 = df["top"].diff().fillna(0).gt(10).cumsum()
df["top_min"] = df.groupby(ind2)["top"].transform("min")
print(df.sort_index())
Output:
left top text left_min top_min
4 66 23 6/22/2021 64 23
6 66 82 6/23/2021 64 82
8 65 142 6/24/2021 64 142
10 65 202 6/25/2021 64 202
12 64 262 6/26/2021 64 262
16 345 25 14:00 343 23
18 354 85 7:30 343 82
20 344 145 13:00 343 142
22 344 206 11:00 343 202
24 343 265 10:00 343 262
26 343 325 15:00 343 324
30 859 23 20:30 858 23
32 860 84 14:00 858 82
34 858 144 20:23 858 142
36 859 204 18:00 858 202
38 858 264 13:00 858 262
40 858 324 18:15 858 324
44 1091 23 6:30 1087 23
46 1091 84 6:30 1087 82
48 1090 144 7:23 1087 142
50 1090 204 7:00 1087 202
52 1089 264 3:00 1087 262
54 1089 324 3:15 1087 324
56 1088 383 0:00 1087 383
58 1087 443 0:00 1087 443
60 1087 503 0:00 1087 503
62 1047 563 33:38:00 1047 563

Scraping a team stats table from python using BS4

Im trying to scrape a table from pro-football-reference, specifically the team offense table from https://www.pro-football-reference.com/years/2019/. Anytime I try the code below I get back an empty list or just a NoneType. I have scraped other websites like ESPN and have had no problems.
import requests
from bs4 import BeautifulSoup
url = 'https://www.pro-football-reference.com/years/{}/'
response = requests.get(url.format(2019))
soup = BeautifulSoup(response.text, 'lxml')
team_table = soup.find('table', {'id':'team_stats'})
I have also tried
soup = BeautifulSoup(response.text, 'html.parser')
to see if maybe it was the way I was bringing the data in. The page does have a bunch of tables so im assuming thats why its more difficult to scrape a specific table. Thank you.
The data is inside HTML comments <!-- ... -->. You can use this script to get them:
import requests
from bs4 import BeautifulSoup, Comment
url = "https://www.pro-football-reference.com/years/2019/"
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
table = soup.select_one('#all_team_stats').find_next(text=lambda t: isinstance(t, Comment))
table = BeautifulSoup(table, 'html.parser')
for tr in table.select('tr'):
tds = [td.get_text(strip=True) for td in tr.select('td')]
print(*tds)
Prints:
Baltimore Ravens 16 531 6521 1064 6.1 15 7 386 289 440 3225 37 8 6.9 171 596 3296 21 5.5 188 109 867 27 52.1 8.6 245.99
San Francisco 49ers 16 479 6097 1012 6.0 23 10 336 331 478 3792 28 13 7.4 195 498 2305 23 4.6 110 105 939 31 44.3 12.0 146.39
Tampa Bay Buccaneers 16 458 6366 1086 5.9 41 11 353 382 630 4845 33 30 7.2 244 409 1521 15 3.7 81 133 1111 28 38.3 20.7 44.00
New Orleans Saints 16 458 5982 1011 5.9 8 2 347 418 581 4244 36 6 7.0 230 405 1738 12 4.3 97 120 1036 20 47.1 4.1 167.04
Kansas City Chiefs 16 451 6067 976 6.2 15 10 350 378 576 4498 30 5 7.5 211 375 1569 16 4.2 93 107 1029 46 49.4 8.0 265.38
Dallas Cowboys 16 434 6904 1069 6.5 18 7 379 388 597 4751 30 11 7.7 229 449 2153 18 4.8 120 109 1008 30 44.6 10.3 214.77
New England Patriots 16 420 5664 1095 5.2 15 6 338 378 620 3961 25 9 6.1 197 447 1703 17 3.8 110 94 828 31 36.8 7.6 39.62
Minnesota Vikings 16 407 5656 970 5.8 20 12 314 319 466 3523 26 8 7.1 171 476 2133 19 4.5 106 96 895 37 41.9 10.5 103.01
Seattle Seahawks 16 405 5991 1046 5.7 20 14 341 341 517 3791 31 6 6.7 190 481 2200 15 4.6 121 109 882 30 36.9 10.2 90.55
Tennessee Titans 16 402 5805 949 6.1 17 9 317 297 448 3582 29 8 7.1 177 445 2223 21 5.0 104 99 932 36 31.7 8.2 119.88
Los Angeles Rams 16 394 5998 1055 5.7 24 7 342 397 632 4499 22 17 6.9 222 401 1499 20 3.7 92 118 899 28 36.0 12.9 42.09
Philadelphia Eagles 16 385 5772 1104 5.2 23 15 354 391 613 3833 27 8 5.9 215 454 1939 16 4.3 104 100 836 35 35.5 10.4 67.85
Atlanta Falcons 16 381 6075 1096 5.5 25 10 383 459 684 4714 29 15 6.4 258 362 1361 10 3.8 84 119 956 41 41.3 13.4 104.51
Houston Texans 16 378 5792 1017 5.7 22 8 346 355 534 3783 27 14 6.5 203 434 2009 17 4.6 112 111 892 31 37.7 12.0 121.67
Green Bay Packers 16 376 5528 1020 5.4 13 9 320 356 573 3733 26 4 6.1 190 411 1795 18 4.4 90 100 774 40 37.1 6.9 118.40
Arizona Cardinals 16 361 5467 1000 5.5 18 6 314 355 554 3477 20 12 5.8 176 396 1990 18 5.0 109 121 956 29 38.8 10.1 67.36
Indianapolis Colts 16 361 5238 1016 5.2 21 11 340 307 513 3108 22 10 5.7 165 471 2130 17 4.5 131 79 670 44 36.1 11.4 78.80
Detroit Lions 16 341 5549 1021 5.4 23 8 313 344 571 3900 28 15 6.4 196 407 1649 7 4.1 82 113 937 35 33.3 11.7 33.07
New York Giants 16 341 5416 1012 5.4 33 16 311 376 607 3731 30 17 5.7 187 362 1685 11 4.7 89 90 784 35 28.3 18.3 -5.30
Carolina Panthers 16 340 5469 1077 5.1 35 14 335 382 633 3650 17 21 5.3 230 386 1819 20 4.7 82 87 754 23 32.3 16.9 -24.07
Los Angeles Chargers 16 337 5879 997 5.9 31 11 349 394 597 4426 24 20 7.0 220 366 1453 12 4.0 90 103 872 39 39.5 18.5 83.43
Cleveland Browns 16 335 5455 973 5.6 28 7 305 318 539 3554 22 21 6.1 180 393 1901 15 4.8 90 122 1106 35 34.1 14.8 16.54
Buffalo Bills 16 314 5283 1018 5.2 19 7 314 299 513 3229 21 12 5.8 162 465 2054 13 4.4 120 117 927 32 30.6 10.4 9.66
Oakland Raiders 16 313 5819 989 5.9 17 9 315 367 523 3926 22 8 7.1 194 437 1893 13 4.3 104 128 1138 17 32.9 9.9 80.57
Miami Dolphins 16 306 4960 1022 4.9 26 8 315 371 615 3804 22 18 5.7 210 349 1156 10 3.3 64 92 769 41 30.6 13.3 -23.85
Jacksonville Jaguars 16 300 5468 1020 5.4 20 12 298 364 589 3760 24 8 6.0 183 389 1708 3 4.4 85 132 1165 30 33.9 10.2 -14.15
Pittsburgh Steelers 16 289 4428 937 4.7 30 11 265 315 510 2981 18 19 5.5 147 395 1447 7 3.7 75 111 893 43 28.6 15.7 -84.56
Denver Broncos 16 282 4777 954 5.0 16 6 279 312 504 3115 16 10 5.7 162 409 1662 11 4.1 77 110 912 40 32.9 9.4 -11.61
Chicago Bears 16 280 4749 1020 4.7 19 7 297 371 580 3291 20 12 5.3 178 395 1458 8 3.7 85 103 838 34 29.1 10.5 -38.05
Cincinnati Bengals 16 279 5169 1049 4.9 30 14 312 356 616 3652 18 16 5.5 191 385 1517 9 3.9 85 93 761 36 30.3 16.0 -57.73
New York Jets 16 276 4368 956 4.6 25 9 253 323 521 3111 19 16 5.4 162 383 1257 6 3.3 61 115 1105 30 23.0 11.5 -108.92
Washington Redskins 16 266 4395 885 5.0 21 8 248 298 479 2812 18 13 5.3 154 356 1583 9 4.4 74 106 835 20 30.1 12.1 -82.30
Avg Team 365.0 5565.8 1016.1 5.5 22.2 9.4 324.0 354.1 557.9 3759.4 24.9 12.8 6.3 193.8 418.3 1806.4 14.0 4.3 97.3 107.8 915.8 32.9 36.0 11.8 56.6
League Total 11680 178107 32516 5.5 711 301 10369 11331 17853 120301 797 410 6.3 6200 13387 57806 447 4.3 3115 3451 29306 1054 36.0 11.8
Avg Tm/G 22.8 347.9 63.5 5.5 1.4 0.6 20.3 22.1 34.9 235.0 1.6 0.8 6.3 12.1 26.1 112.9 0.9 4.3 6.1 6.7 57.2 2.1 36.0 11.8

X-Axis Ticks labels by year with X-Axis gridlines by fiscal quarter

I am trying to set the x axis tick labels as the year but have the gridlines as the fiscal quarter. The data is quite simple, just a groupby date.count, see below. Each date has a count and I am plotting it as a line plot.
rc[(rc['form']=='Bakken')&(rc['tgt']=='oil')].groupby(['date']).date.count()
date count
2010-01-08 65
2010-01-15 68
2010-01-22 73
2010-01-29 76
2010-02-05 79
2010-02-12 76
2010-02-19 79
2010-02-26 83
2010-03-05 81
2010-03-12 83
2010-03-19 80
2010-03-26 87
2010-04-02 84
2010-04-09 87
2010-04-16 87
2010-04-23 91
2010-04-30 86
2010-05-07 92
2010-05-14 95
2010-05-21 91
2010-05-28 100
2010-06-04 96
2010-06-11 101
2010-06-18 100
2010-06-25 113
2010-07-02 112
2010-07-09 119
2010-07-16 121
2010-07-23 119
2010-07-30 115
2010-08-06 115
2010-08-13 114
2010-08-20 111
2010-08-27 114
2010-09-03 121
2010-09-10 128
2010-09-17 121
2010-09-24 118
2010-10-01 109
2010-10-08 120
2010-10-15 122
2010-10-22 120
2010-10-29 118
2010-11-05 117
2010-11-12 115
2010-11-19 113
2010-11-26 106
2010-12-03 112
2010-12-10 114
2010-12-17 122
2010-12-24 120
2010-12-31 120
2011-01-07 139
2011-01-14 141
2011-01-21 141
2011-01-28 145
2011-02-04 146
2011-02-11 145
2011-02-18 148
2011-02-25 149
2011-03-04 150
2011-03-11 149
2011-03-18 145
2011-03-25 140
2011-04-01 150
2011-04-08 153
2011-04-15 151
2011-04-22 148
2011-04-29 150
2011-05-06 148
2011-05-13 154
2011-05-20 155
2011-05-27 152
2011-06-03 158
2011-06-10 155
2011-06-17 152
2011-06-24 148
2011-07-01 160
2011-07-08 164
2011-07-15 163
2011-07-22 147
2011-07-29 158
2011-08-05 161
2011-08-12 166
2011-08-19 158
2011-08-26 154
2011-09-02 161
2011-09-09 166
2011-09-16 160
2011-09-23 169
2011-09-30 171
2011-10-07 155
2011-10-14 159
2011-10-21 156
2011-10-28 168
2011-11-04 154
2011-11-11 166
2011-11-18 168
2011-11-25 164
2011-12-02 179
2011-12-09 171
2011-12-16 172
2011-12-23 165
2011-12-30 170
2012-01-06 162
2012-01-13 172
2012-01-20 172
2012-01-27 186
2012-02-03 183
2012-02-10 175
2012-02-17 188
2012-02-24 182
2012-03-02 184
2012-03-09 189
2012-03-16 190
2012-03-23 181
2012-03-30 186
2012-04-06 180
2012-04-13 178
2012-04-20 179
2012-04-27 174
2012-05-04 201
2012-05-11 201
2012-05-18 201
2012-05-25 201
2012-06-01 206
2012-06-08 206
2012-06-15 199
2012-06-22 201
2012-06-29 186
2012-07-06 194
2012-07-13 192
2012-07-20 189
2012-07-27 189
2012-08-03 189
2012-08-10 194
2012-08-17 190
2012-08-24 192
2012-08-31 177
2012-09-07 186
2012-09-14 173
2012-09-21 178
2012-09-28 180
2012-10-05 173
2012-10-12 165
2012-10-19 167
2012-10-26 160
2012-11-02 160
2012-11-09 167
2012-11-16 159
2012-11-23 161
2012-11-30 166
2012-12-07 161
2012-12-14 150
2012-12-21 158
2012-12-28 122
2013-01-04 121
2013-01-11 115
2013-01-18 116
2013-01-25 119
2013-02-01 113
2013-02-08 112
2013-02-15 125
2013-02-22 113
2013-03-01 117
2013-03-08 113
2013-03-15 113
2013-03-22 116
2013-03-29 125
2013-04-05 113
2013-04-12 120
2013-04-19 120
2013-04-26 128
2013-05-03 131
2013-05-10 129
2013-05-17 135
2013-05-24 125
2013-05-31 140
2013-06-07 131
2013-06-14 129
2013-06-21 130
2013-06-28 139
2013-07-05 136
2013-07-12 137
2013-07-19 131
2013-07-26 132
2013-08-02 131
2013-08-09 138
2013-08-16 138
2013-08-23 140
2013-08-30 137
2013-09-06 132
2013-09-13 132
2013-09-20 129
2013-09-27 129
2013-10-04 128
2013-10-11 129
2013-10-18 130
2013-10-25 135
2013-11-01 128
2013-11-08 131
2013-11-15 130
2013-11-22 128
2013-11-29 134
2013-12-06 140
2013-12-13 131
2013-12-20 130
2013-12-27 125
2014-01-03 134
2014-01-10 138
2014-01-17 139
2014-01-24 129
2014-01-31 142
2014-02-07 145
2014-02-14 135
2014-02-21 140
2014-02-28 137
2014-03-07 148
2014-03-14 148
2014-03-21 140
2014-03-28 141
2014-04-04 148
2014-04-11 145
2014-04-18 145
2014-04-25 140
2014-05-02 157
2014-05-09 146
2014-05-16 143
2014-05-23 159
2014-05-30 152
2014-06-06 141
2014-06-13 145
2014-06-20 152
2014-06-27 145
2014-07-03 144
2014-07-11 150
2014-07-18 145
2014-07-25 146
2014-08-01 149
2014-08-08 145
2014-08-15 146
2014-08-22 151
2014-08-29 142
2014-09-05 155
2014-09-12 149
2014-09-19 158
2014-09-26 149
2014-10-03 154
2014-10-10 141
2014-10-17 150
2014-10-24 135
2014-10-31 145
2014-11-07 145
2014-11-14 155
2014-11-21 143
2014-11-26 148
2014-12-05 149
2014-12-12 151
2014-12-19 155
2014-12-26 143
2015-01-02 131
2015-01-09 132
2015-01-16 124
2015-01-23 132
2015-01-30 121
2015-02-06 116
2015-02-13 115
2015-02-20 105
2015-02-27 77
2015-03-06 73
2015-03-13 72
2015-03-20 65
2015-03-27 64
2015-04-03 65
2015-04-10 62
2015-04-17 61
2015-04-24 59
2015-05-01 56
2015-05-08 58
2015-05-15 54
2015-05-22 53
2015-05-29 50
2015-06-05 50
2015-06-12 52
2015-06-19 54
2015-06-26 52
2015-07-02 50
2015-07-10 48
2015-07-17 45
2015-07-24 44
2015-07-31 43
2015-08-07 42
2015-08-14 45
2015-08-21 45
2015-08-28 47
2015-09-04 46
2015-09-11 43
2015-09-18 43
2015-09-25 44
2015-10-02 44
2015-10-09 44
2015-10-16 40
2015-10-23 38
2015-10-30 39
2015-11-06 32
2015-11-13 30
2015-11-20 31
2015-11-27 28
2015-12-04 31
2015-12-11 26
2015-12-18 26
2015-12-25 28
2016-01-01 25
2016-01-08 26
2016-01-15 25
2016-01-22 21
2016-01-29 23
2016-02-05 20
2016-02-12 21
2016-02-19 37
2016-02-26 34
2016-03-04 32
2016-03-11 31
2016-03-18 32
2016-03-24 30
2016-04-01 27
2016-04-08 25
2016-04-15 23
2016-04-22 23
lanery pointed to right place. you need to define you quarters and use in the same fashion.
Define years
years = ['2009-12-31', '2010-12-31', '2011-12-30', '2012-12-31',
'2013-12-31', '2014-12-31', '2015-12-31']
Define quarters
quarters = ['2009-12-31', '2010-03-31', '2010-06-30', '2010-09-30',
'2010-12-31', '2011-03-31', '2011-06-30', '2011-09-30',
'2011-12-30', '2012-03-30', '2012-06-29', '2012-09-28',
'2012-12-31', '2013-03-29', '2013-06-28', '2013-09-30',
'2013-12-31', '2014-03-31', '2014-06-30', '2014-09-30',
'2014-12-31', '2015-03-31', '2015-06-30', '2015-09-30',
'2015-12-31', '2016-03-31']
Load the data you supplied
import pandas as pd
from StringIO import StringIO
text = """date count
2010-01-08 65
2010-01-15 68
2010-01-22 73
2010-01-29 76
2010-02-05 79
2010-02-12 76
2010-02-19 79
2010-02-26 83
2010-03-05 81
2010-03-12 83
2010-03-19 80
2010-03-26 87
2010-04-02 84
2010-04-09 87
2010-04-16 87
2010-04-23 91
2010-04-30 86
2010-05-07 92
2010-05-14 95
2010-05-21 91
2010-05-28 100
2010-06-04 96
2010-06-11 101
2010-06-18 100
2010-06-25 113
2010-07-02 112
2010-07-09 119
2010-07-16 121
2010-07-23 119
2010-07-30 115
2010-08-06 115
2010-08-13 114
2010-08-20 111
2010-08-27 114
2010-09-03 121
2010-09-10 128
2010-09-17 121
2010-09-24 118
2010-10-01 109
2010-10-08 120
2010-10-15 122
2010-10-22 120
2010-10-29 118
2010-11-05 117
2010-11-12 115
2010-11-19 113
2010-11-26 106
2010-12-03 112
2010-12-10 114
2010-12-17 122
2010-12-24 120
2010-12-31 120
2011-01-07 139
2011-01-14 141
2011-01-21 141
2011-01-28 145
2011-02-04 146
2011-02-11 145
2011-02-18 148
2011-02-25 149
2011-03-04 150
2011-03-11 149
2011-03-18 145
2011-03-25 140
2011-04-01 150
2011-04-08 153
2011-04-15 151
2011-04-22 148
2011-04-29 150
2011-05-06 148
2011-05-13 154
2011-05-20 155
2011-05-27 152
2011-06-03 158
2011-06-10 155
2011-06-17 152
2011-06-24 148
2011-07-01 160
2011-07-08 164
2011-07-15 163
2011-07-22 147
2011-07-29 158
2011-08-05 161
2011-08-12 166
2011-08-19 158
2011-08-26 154
2011-09-02 161
2011-09-09 166
2011-09-16 160
2011-09-23 169
2011-09-30 171
2011-10-07 155
2011-10-14 159
2011-10-21 156
2011-10-28 168
2011-11-04 154
2011-11-11 166
2011-11-18 168
2011-11-25 164
2011-12-02 179
2011-12-09 171
2011-12-16 172
2011-12-23 165
2011-12-30 170
2012-01-06 162
2012-01-13 172
2012-01-20 172
2012-01-27 186
2012-02-03 183
2012-02-10 175
2012-02-17 188
2012-02-24 182
2012-03-02 184
2012-03-09 189
2012-03-16 190
2012-03-23 181
2012-03-30 186
2012-04-06 180
2012-04-13 178
2012-04-20 179
2012-04-27 174
2012-05-04 201
2012-05-11 201
2012-05-18 201
2012-05-25 201
2012-06-01 206
2012-06-08 206
2012-06-15 199
2012-06-22 201
2012-06-29 186
2012-07-06 194
2012-07-13 192
2012-07-20 189
2012-07-27 189
2012-08-03 189
2012-08-10 194
2012-08-17 190
2012-08-24 192
2012-08-31 177
2012-09-07 186
2012-09-14 173
2012-09-21 178
2012-09-28 180
2012-10-05 173
2012-10-12 165
2012-10-19 167
2012-10-26 160
2012-11-02 160
2012-11-09 167
2012-11-16 159
2012-11-23 161
2012-11-30 166
2012-12-07 161
2012-12-14 150
2012-12-21 158
2012-12-28 122
2013-01-04 121
2013-01-11 115
2013-01-18 116
2013-01-25 119
2013-02-01 113
2013-02-08 112
2013-02-15 125
2013-02-22 113
2013-03-01 117
2013-03-08 113
2013-03-15 113
2013-03-22 116
2013-03-29 125
2013-04-05 113
2013-04-12 120
2013-04-19 120
2013-04-26 128
2013-05-03 131
2013-05-10 129
2013-05-17 135
2013-05-24 125
2013-05-31 140
2013-06-07 131
2013-06-14 129
2013-06-21 130
2013-06-28 139
2013-07-05 136
2013-07-12 137
2013-07-19 131
2013-07-26 132
2013-08-02 131
2013-08-09 138
2013-08-16 138
2013-08-23 140
2013-08-30 137
2013-09-06 132
2013-09-13 132
2013-09-20 129
2013-09-27 129
2013-10-04 128
2013-10-11 129
2013-10-18 130
2013-10-25 135
2013-11-01 128
2013-11-08 131
2013-11-15 130
2013-11-22 128
2013-11-29 134
2013-12-06 140
2013-12-13 131
2013-12-20 130
2013-12-27 125
2014-01-03 134
2014-01-10 138
2014-01-17 139
2014-01-24 129
2014-01-31 142
2014-02-07 145
2014-02-14 135
2014-02-21 140
2014-02-28 137
2014-03-07 148
2014-03-14 148
2014-03-21 140
2014-03-28 141
2014-04-04 148
2014-04-11 145
2014-04-18 145
2014-04-25 140
2014-05-02 157
2014-05-09 146
2014-05-16 143
2014-05-23 159
2014-05-30 152
2014-06-06 141
2014-06-13 145
2014-06-20 152
2014-06-27 145
2014-07-03 144
2014-07-11 150
2014-07-18 145
2014-07-25 146
2014-08-01 149
2014-08-08 145
2014-08-15 146
2014-08-22 151
2014-08-29 142
2014-09-05 155
2014-09-12 149
2014-09-19 158
2014-09-26 149
2014-10-03 154
2014-10-10 141
2014-10-17 150
2014-10-24 135
2014-10-31 145
2014-11-07 145
2014-11-14 155
2014-11-21 143
2014-11-26 148
2014-12-05 149
2014-12-12 151
2014-12-19 155
2014-12-26 143
2015-01-02 131
2015-01-09 132
2015-01-16 124
2015-01-23 132
2015-01-30 121
2015-02-06 116
2015-02-13 115
2015-02-20 105
2015-02-27 77
2015-03-06 73
2015-03-13 72
2015-03-20 65
2015-03-27 64
2015-04-03 65
2015-04-10 62
2015-04-17 61
2015-04-24 59
2015-05-01 56
2015-05-08 58
2015-05-15 54
2015-05-22 53
2015-05-29 50
2015-06-05 50
2015-06-12 52
2015-06-19 54
2015-06-26 52
2015-07-02 50
2015-07-10 48
2015-07-17 45
2015-07-24 44
2015-07-31 43
2015-08-07 42
2015-08-14 45
2015-08-21 45
2015-08-28 47
2015-09-04 46
2015-09-11 43
2015-09-18 43
2015-09-25 44
2015-10-02 44
2015-10-09 44
2015-10-16 40
2015-10-23 38
2015-10-30 39
2015-11-06 32
2015-11-13 30
2015-11-20 31
2015-11-27 28
2015-12-04 31
2015-12-11 26
2015-12-18 26
2015-12-25 28
2016-01-01 25
2016-01-08 26
2016-01-15 25
2016-01-22 21
2016-01-29 23
2016-02-05 20
2016-02-12 21
2016-02-19 37
2016-02-26 34
2016-03-04 32
2016-03-11 31
2016-03-18 32
2016-03-24 30
2016-04-01 27
2016-04-08 25
2016-04-15 23
2016-04-22 23"""
Parse your data
data = pd.read_csv(StringIO(text), index_col=[0], parse_dates=[0], delim_whitespace=True)
Use info from
How to add a grid line at a specific location in matplotlib plot?
fig, ax = plt.subplots()
ax.set_xticks(quarters, minor=True)
ax.set_xticks(years, minor=False)
ax.xaxis.grid(True, which='minor')
ax.xaxis.grid(False, which='major')
data.plot(ax=ax)

Categories

Resources