简体   繁体   English

如何使用 pandas 按行和列组织 dataframe?

[英]How do I use pandas to organize dataframe by both row and column?

I'm learning python and pandas, and I know how to do basic operations like groupby() and sum().我正在学习 python 和 pandas,并且我知道如何进行 groupby() 和 sum() 之类的基本操作。 But I'm trying to do more complex operations like categorizing using rows and columns, but I'm not sure how to begin the problem below.但我正在尝试做更复杂的操作,比如使用行和列进行分类,但我不确定如何开始下面的问题。

Here's the dataset from GitHub: https://github.com/KeithGalli/pandas/blob/master/pokemon_data.csv这是来自 GitHub 的数据集: https://github.com/KeithGalli/pandas/blob/master/pokemon_data.csv

Here's what I'm trying to produce:这是我要制作的内容:

Generation一代 Fire AM消防调幅 Fire NZ消防新西兰 Water AM水 AM Water NZ新西兰水 Grass AM草上午 Grass NZ草新西兰
1 1 #pokemon #口袋妖怪
2 2
3 3
4 4
5 5
6 6

Here's what my approach:这是我的方法:

df = pd.read_csv(pokemon_data.csv, header=0)

fire = df.loc[df['Type 1'] == 'Fire']
water = df.loc[df['Type 1'] == 'Water']
grass = df.loc[df['Type 1'] == 'Grass']

# Trim down columns to only related data
fire = fire[['Name', 'Type 1', 'Generation']]
water = water[['Name', 'Type 1', 'Generation']]
grass = grass[['Name', 'Type 1', 'Generation']]

Next steps: Should I begin to sort by Generation first, or by alphabetical range (AM and NZ)?下一步:我应该先按 Generation 排序,还是按字母范围(AM 和 NZ)排序? I can't wrap my head around this.我无法解决这个问题。

An explanation of your work is much appreciated.非常感谢您对您的工作的解释。 Thank you!谢谢!

Create helper column for new columns in final DataFrame by compare first letter of column Name and then use DataFrame.pivot_table , if need aggregate strings in Name need aggregate function join :通过比较列Name的第一个字母,然后使用DataFrame.pivot_table为最终 DataFrame 中的新列创建帮助器列,如果需要在Name中聚合字符串需要聚合join

df['cat'] = df['Type 1'] + ' ' + np.where(df['Name'].str[0].gt('M'), 'N-Z','A-M')
print (df)
       #                   Name   Type 1  Type 2  HP  Attack  Defense  \
0      1              Bulbasaur    Grass  Poison  45      49       49   
1      2                Ivysaur    Grass  Poison  60      62       63   
2      3               Venusaur    Grass  Poison  80      82       83   
3      3  VenusaurMega Venusaur    Grass  Poison  80     100      123   
4      4             Charmander     Fire     NaN  39      52       43   
..   ...                    ...      ...     ...  ..     ...      ...   
795  719                Diancie     Rock   Fairy  50     100      150   
796  719    DiancieMega Diancie     Rock   Fairy  50     160      110   
797  720    HoopaHoopa Confined  Psychic   Ghost  80     110       60   
798  720     HoopaHoopa Unbound  Psychic    Dark  80     160       60   
799  721              Volcanion     Fire   Water  80     110      120   

     Sp. Atk  Sp. Def  Speed  Generation  Legendary          cat  
0         65       65     45           1      False    Grass A-M  
1         80       80     60           1      False    Grass A-M  
2        100      100     80           1      False    Grass N-Z  
3        122      120     80           1      False    Grass N-Z  
4         60       50     65           1      False     Fire A-M  
..       ...      ...    ...         ...        ...          ...  
795      100      150     50           6       True     Rock A-M  
796      160      110    110           6       True     Rock A-M  
797      150      130     70           6       True  Psychic A-M  
798      170      130     80           6       True  Psychic A-M  
799      130       90     70           6       True     Fire N-Z  

df = df.pivot_table(index='Generation', columns='cat', values='Name', aggfunc=','.join)
# print (df)

Create your column names first then pivot your dataframe:首先创建列名,然后创建 pivot 您的 dataframe:

df['Group'] = df['Type 1'] + ' ' + np.where(df['Name'].str[0].between('A', 'M'), 'A-M', 'N-Z')
out = df.astype({'#': str}).pivot_table('#', 'Generation', 'Group', aggfunc=' '.join)

Output Output

>>> out
Group                                           Bug A-M                      Bug N-Z             Dark A-M  ...    Steel N-Z                                          Water A-M                                          Water N-Z
Generation                                                                                                 ...                                                                                                                   
1                                     10 11 12 14 15 15   13 46 47 48 49 123 127 127                  NaN  ...          NaN         9 9 55 87 91 98 99 116 118 129 130 130 131  7 8 54 60 61 62 72 73 79 80 80 86 90 117 119 1...
2                               165 166 168 205 214 214      167 193 204 212 212 213      198 228 229 229  ...  208 208 227                159 160 170 171 183 184 222 226 230                158 186 194 195 199 211 223 224 245
3                                   267 268 269 284 314  265 266 283 290 291 292 313          262 359 359  ...          379  258 259 270 271 272 318 339 341 342 349 350 36...            260 260 278 279 319 319 320 321 340 369
4                                   401 402 412 414 415          413 413 413 416 469              430 491  ...          NaN                    395 418 419 423 456 457 458 490                                393 394 422 484 489
5           542 557 558 588 589 595 596 617 632 636 649  540 541 543 544 545 616 637  510 625 630 633 635  ...          NaN                502 550 565 580 592 593 594 647 647                501 503 515 516 535 536 537 564 581
6                                                   NaN                  664 665 666              686 687  ...          NaN                                656 657 658 692 693                                                NaN

[6 rows x 35 columns]

Transposed view for readability:转置视图以提高可读性:

>>> out.T
Generation                                                    1                                    2                                                  3                                                4                                            5                            6
Group                                                                                                                                                                                                                                                                             
Bug A-M                                       10 11 12 14 15 15              165 166 168 205 214 214                                267 268 269 284 314                              401 402 412 414 415  542 557 558 588 589 595 596 617 632 636 649                          NaN
Bug N-Z                              13 46 47 48 49 123 127 127              167 193 204 212 212 213                        265 266 283 290 291 292 313                              413 413 413 416 469                  540 541 543 544 545 616 637                  664 665 666
Dark A-M                                                    NaN                      198 228 229 229                                        262 359 359                                          430 491                          510 625 630 633 635                      686 687
Dark N-Z                                                    NaN                              197 215                                        261 302 302                                              461              509 559 560 570 571 624 629 634                          717
Dragon A-M                                          147 148 149                                  NaN                        334 334 371 380 380 381 381                                  443 444 445 445                  610 611 612 621 646 646 646                      704 706
Dragon N-Z                                                  NaN                                  NaN                                372 373 373 384 384                                              NaN                                      643 644                      705 718
Electric A-M                                  81 82 101 125 135                  179 180 181 181 239                                    309 310 310 312                                  404 405 462 466                              522 587 603 604                  694 695 702
Electric N-Z                                      25 26 100 145                              172 243                                                311                  403 417 479 479 479 479 479 479                              523 602 642 642                          NaN
Fairy A-M                                                 35 36                              173 210                                                NaN                                              NaN                                          NaN              669 670 671 683
Fairy N-Z                                                   NaN                          175 176 209                                                NaN                                              468                                          NaN          682 684 685 700 716
Fighting A-M                                56 66 67 68 106 107                                  237                                296 297 307 308 308                                          448 448                              533 534 619 620                          701
Fighting N-Z                                                 57                                  236                                                NaN                                              447                                  532 538 539                      674 675
Fire A-M                            4 5 6 6 6 58 59 126 136 146                  155 219 240 244 250                                256 257 257 323 323                              390 391 392 467 485                          500 554 555 555 631          653 654 655 662 667
Fire N-Z                                            37 38 77 78                          156 157 218                                        255 322 324                                              NaN                              498 499 513 514                  663 668 721
Flying N-Z                                                  NaN                                  NaN                                                NaN                                              NaN                                      641 641                      714 715
Ghost A-M                                           92 93 94 94                                  200                                    354 354 355 356                          425 426 429 477 487 487                              563 607 608 609              711 711 711 711
Ghost N-Z                                                   NaN                                  NaN                                                353                                              442                                          562      708 709 710 710 710 710
Grass A-M                                     1 2 44 69 102 103              152 153 154 182 187 189                                    253 286 331 332                  388 406 420 421 455 460 460 470                  546 549 556 590 591 597 598                  650 652 673
Grass N-Z                                   3 3 43 45 70 71 114                          188 191 192                252 254 254 273 274 275 285 315 357                      387 389 407 459 465 492 492              495 496 497 511 512 547 548 640                      651 672
Ground A-M                                        50 51 104 105                              207 232                                330 343 344 383 383                                      449 450 472              529 530 552 553 622 623 645 645                          NaN
Ground N-Z                                        27 28 111 112                                  231                                            328 329                                              464                                      551 618                          NaN
Ice A-M                                                 124 144                                  225                                            362 362                                      471 473 478                                  613 614 615                      712 713
Ice N-Z                                                     NaN                          220 221 238                                361 363 364 365 378                                              NaN                                  582 583 584                          NaN
Normal A-M            22 39 52 83 84 85 108 113 115 115 132 133      162 163 174 190 203 206 241 242                        264 294 295 298 301 351 352  399 400 424 427 428 428 431 440 441 446 463 493  506 507 531 531 572 573 585 626 628 648 648              659 660 661 676
Normal N-Z               16 17 18 18 19 20 21 40 53 128 137 143          161 164 216 217 233 234 235        263 276 277 287 288 289 293 300 327 333 335                          396 397 398 432 474 486              504 505 508 519 520 521 586 627                          NaN
Poison A-M                                   23 24 42 88 89 109                                  169                                                316                                          452 453                                          569                          691
Poison N-Z                             29 30 31 32 33 34 41 110                                  NaN                                            317 336                                  434 435 451 454                                          568                          690
Psychic A-M               63 64 65 65 96 97 122 150 150 150 151                          196 249 251                281 282 282 326 358 386 386 386 386                      433 439 475 475 481 482 488              517 518 574 575 576 578 605 606          677 678 678 720 720
Psychic N-Z                                                 NaN                      177 178 201 202                                        280 325 360                                              480                      494 527 528 561 577 579                          NaN
Rock A-M                               74 75 76 140 141 142 142                                  246                                337 345 346 347 348                                      408 411 438                              525 526 566 567  688 689 698 699 703 719 719
Rock N-Z                                             95 138 139                      185 247 248 248                                        299 338 377                                      409 410 476                                      524 639                      696 697
Steel A-M                                                   NaN                                  NaN        303 303 304 305 306 306 374 375 376 376 385                                      436 437 483                              599 600 601 638          679 680 681 681 707
Steel N-Z                                                   NaN                          208 208 227                                                379                                              NaN                                          NaN                          NaN
Water A-M            9 9 55 87 91 98 99 116 118 129 130 130 131  159 160 170 171 183 184 222 226 230  258 259 270 271 272 318 339 341 342 349 350 36...                  395 418 419 423 456 457 458 490          502 550 565 580 592 593 594 647 647          656 657 658 692 693
Water N-Z     7 8 54 60 61 62 72 73 79 80 80 86 90 117 119 1...  158 186 194 195 199 211 223 224 245            260 260 278 279 319 319 320 321 340 369                              393 394 422 484 489          501 503 515 516 535 536 537 564 581                          NaN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在使用 pandas dataframe 时可以使用方括号来引用列标签和行索引? - Why can I use square brackets to refer to both column labels and row indices when using a pandas dataframe? 如何在pandas数据帧的第二行中添加列标题? - How do i add column header, in the second row in a pandas dataframe? 我如何使用 python pandas 数据框并使用列名和行名作为新列创建一个新表 - how do i take a python pandas dataframe and create a new table using the column and row names as the new column 如何从包含给定字符串的行开始切片两列熊猫数据帧? - How do I slice a two column pandas dataframe starting with a row containing a given string? 如何遍历 Pandas.DataFrame 和 append 中的一列 ZC1C425Z078E683894FC1AB4 的结果到同一行 17? - How do I iterate over a column in a Pandas.DataFrame and append the result of a function to the same row? 如何向 pandas dataframe 添加一列,该列在一个范围内具有最高值但将其应用于每一行? - How do I add a column to a pandas dataframe which has the highest value in a range but applying it to every row? 在 Pandas 中,如何同时将 dataframe 中的值与其行和列中的其他值进行比较? - In Pandas, how do I compare values in a dataframe with others in its row and column at the same time? 如何将 pandas Dataframe 列分组到 MultiIndex(按行或按列)? - How do I group pandas Dataframe columns into MultiIndex (row or column wise)? 如何从Excel获取行,然后将其作为列放入Pandas数据框中? - How do I get row from Excel and then put it into a Pandas dataframe as a column? 如何根据每行中的条件将多个字符串添加到 pandas dataframe 中的列中? - How do I add multiple strings to a column in a pandas dataframe based on conditions in each row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM