import pandas as pd
import numpy as np
pd. __version__
'1.0.3'
df = pd. read_csv( 'C:/SAIC/DataHunter/GitHub/joyful-pandas/data/table.csv' )
df. head( )
School Class ID Gender Address Height Weight Math Physics 0 S_1 C_1 1101 M street_1 173 63 34.0 A+ 1 S_1 C_1 1102 F street_2 192 73 32.5 B+ 2 S_1 C_1 1103 M street_2 186 82 87.2 B+ 3 S_1 C_1 1104 F street_2 167 81 80.4 B- 4 S_1 C_1 1105 F street_4 159 64 84.8 B+
df = pd. read_csv( r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\table.csv" )
df. head( )
School Class ID Gender Address Height Weight Math Physics 0 S_1 C_1 1101 M street_1 173 63 34.0 A+ 1 S_1 C_1 1102 F street_2 192 73 32.5 B+ 2 S_1 C_1 1103 M street_2 186 82 87.2 B+ 3 S_1 C_1 1104 F street_2 167 81 80.4 B- 4 S_1 C_1 1105 F street_4 159 64 84.8 B+
df_txt = pd. read_table( r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\table.txt" )
df_txt
col1 col2 col3 col4 0 2 a 1.4 apple 1 3 b 3.4 banana 2 6 c 2.5 orange 3 5 d 3.2 lemon
df_excel = pd. read_excel( r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\table.xlsx" )
df_excel. head( )
School Class ID Gender Address Height Weight Math Physics 0 S_1 C_1 1101 M street_1 173 63 34.0 A+ 1 S_1 C_1 1102 F street_2 192 73 32.5 B+ 2 S_1 C_1 1103 M street_2 186 82 87.2 B+ 3 S_1 C_1 1104 F street_2 167 81 80.4 B- 4 S_1 C_1 1105 F street_4 159 64 84.8 B+
df. to_csv( r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\new_table3.csv" )
df. to_csv( r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\new_table4.csv" , index = False )
df. head( )
School Class ID Gender Address Height Weight Math Physics 0 S_1 C_1 1101 M street_1 173 63 34.0 A+ 1 S_1 C_1 1102 F street_2 192 73 32.5 B+ 2 S_1 C_1 1103 M street_2 186 82 87.2 B+ 3 S_1 C_1 1104 F street_2 167 81 80.4 B- 4 S_1 C_1 1105 F street_4 159 64 84.8 B+
df. apply ( lambda x: str ( x) + 'i' ) . head( 3 )
School 0 S_1\n1 S_1\n2 S_1\n3 S_1\n4 ...
Class 0 C_1\n1 C_1\n2 C_1\n3 C_1\n4 ...
ID 0 1101\n1 1102\n2 1103\n3 1104...
dtype: object
df[ 'Math' ] . apply ( lambda x: str ( x) + 'i' ) . head( 3 )
0 34.0i
1 32.5i
2 87.2i
Name: Math, dtype: object
df[ 'Math' ] . nlargest( 3 )
df[ 'Math' ] . nsmallest( 2 )
10 31.5
1 32.5
Name: Math, dtype: float64
df[ 'Math' ] . idxmax
<bound method Series.idxmax of 0 34.0
1 32.5
2 87.2
3 80.4
4 84.8
5 97.0
6 63.5
7 58.8
8 33.8
9 68.4
10 31.5
11 87.7
12 49.7
13 85.2
14 61.7
15 83.3
16 50.6
17 52.5
18 72.2
19 34.2
20 39.1
21 68.5
22 73.8
23 47.2
24 85.4
25 72.3
26 32.7
27 65.9
28 95.5
29 48.9
30 45.3
31 48.7
32 59.7
33 67.7
34 47.6
Name: Math, dtype: float64>
df. mean( )
ID 1803.000000
Height 174.142857
Weight 74.657143
Math 61.351429
dtype: float64
df. mean( axis = 1 )
0 342.750
1 349.875
2 364.550
3 358.100
4 353.200
5 388.500
6 383.875
7 368.700
8 365.700
9 375.850
10 390.375
11 405.425
12 405.675
13 413.550
14 405.675
15 610.575
16 593.650
17 593.375
18 608.050
19 597.550
20 633.275
21 635.375
22 630.700
23 625.050
24 637.350
25 652.075
26 648.425
27 664.475
28 661.125
29 653.475
30 675.075
31 674.675
32 670.175
33 678.925
34 674.900
dtype: float64
game = pd. read_csv( r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\Game_of_Thrones_Script.csv" )
game. head( )
Release Date Season Episode Episode Title Name Sentence 0 2011/4/17 Season 1 Episode 1 Winter is Coming waymar royce What do you expect? They're savages. One lot s... 1 2011/4/17 Season 1 Episode 1 Winter is Coming will I've never seen wildlings do a thing like this... 2 2011/4/17 Season 1 Episode 1 Winter is Coming waymar royce How close did you get? 3 2011/4/17 Season 1 Episode 1 Winter is Coming will Close as any man would. 4 2011/4/17 Season 1 Episode 1 Winter is Coming gared We should head back to the wall.
game[ 'Name' ] . nunique( )
564
game[ 'Name' ] . value_counts( ) . nlargest( 1 )
tyrion lannister 1760
Name: Name, dtype: int64
longestword = game[ 'Sentence' ] . apply ( lambda x: len ( x. split( ) ) ) . idxmax( )
game. iat[ longestword, 4 ]
game. iat[ longestword, 5 ]
"When I was twelve, my mother and father went to a wedding. Weddings in Volantis last for days, you know. And they left me with my little brother. The second afternoon they were gone was the hottest day in the three-year summer. We couldn't bear to be inside, so we ran down to the Rhoyne. Every child in Volantis was in the Rhoyne that day. The rich, the poor, we were all there. Naked, screaming, racing to the little islands. Drummers were playing for coppers on the east bank. I was treading water, talking to a friend, when I realized I hadn't seen my brother. I called his name. Then I started screaming his name. And then I saw him floating face down. My heart just stopped. I was I dragged him from the water. My friend helped me, I think. I don't even remember. He was so little. Then we pulled him onto the riverbank. And I screamed at him and I shook him. And he was dead. Just dead. A man ran over. He had a fish tattoo on his face. In Volantis, the slaves have tattoos. So you know what they are without having to talk to them. And this man worked on a fishing boat. And he pushed me out of the way. You have to understand, for a slave to push a highborn girl, that's death for the man, a terrible death. But he pushed me out of the way, and he started pressing on my brother's chest again and again and again, until my brother spat out half of the Rhoyne and cried out. And the man cradled his head and told him to be calm. I decided two things that day. I would not waste my years planning dances and masquerades with the other noble ladies. And when I came of age, I would never live in a slave city again. I'm sorry, Your Grace. You told me of your problems and I've blathered on."
df = pd. read_csv( r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\Game_of_Thrones_Script.csv" )
df_words = df. assign( Words= df[ 'Sentence' ] . apply ( lambda x: len ( x. split( ) ) ) ) . sort_values( by= 'Name' )
df_words. head( )
Release Date Season Episode Episode Title Name Sentence Words 276 2011/4/17 Season 1 Episode 1 Winter is Coming a voice It's Maester Luwin, my lord. 5 3012 2011/6/19 Season 1 Episode 10 Fire and Blood addam marbrand ls it true about Stannis and Renly? 7 3017 2011/6/19 Season 1 Episode 10 Fire and Blood addam marbrand Kevan Lannister 2 13610 2014/6/8 Season 4 Episode 9 The Watchers on the Wall aemon And what is it that couldn't wait until mornin... 10 13614 2014/6/8 Season 4 Episode 9 The Watchers on the Wall aemon Oh, no need. I know my way around this library... 48
L_count = [ ]
N_words = list ( zip ( df_words[ 'Name' ] , df_words[ 'Words' ] ) )
for i in N_words:
if i == N_words[ 0 ] :
L_count. append( i[ 1 ] )
last = i[ 0 ]
else :
L_count. append( L_count[ - 1 ] + i[ 1 ] if i[ 0 ] == last else i[ 1 ] )
last = i[ 0 ]
df_words[ 'Count' ] = L_count
df_words[ 'Name' ] [ df_words[ 'Count' ] . idxmax( ) ]
'tyrion lannister'
df_words[ 'Sentence' ] [ df_words[ 'Count' ] . idxmax( ) ]
"Where? I don't know where, but when they free me -"
df = pd. read_csv( r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\Kobe_data.csv" , index_col= 'shot_id' )
df. head( )
action_type combined_shot_type game_event_id game_id lat loc_x loc_y lon minutes_remaining period ... shot_made_flag shot_type shot_zone_area shot_zone_basic shot_zone_range team_id team_name game_date matchup opponent shot_id 1 Jump Shot Jump Shot 10 20000012 33.9723 167 72 -118.1028 10 1 ... NaN 2PT Field Goal Right Side(R) Mid-Range 16-24 ft. 1610612747 Los Angeles Lakers 2000/10/31 LAL @ POR POR 2 Jump Shot Jump Shot 12 20000012 34.0443 -157 0 -118.4268 10 1 ... 0.0 2PT Field Goal Left Side(L) Mid-Range 8-16 ft. 1610612747 Los Angeles Lakers 2000/10/31 LAL @ POR POR 3 Jump Shot Jump Shot 35 20000012 33.9093 -101 135 -118.3708 7 1 ... 1.0 2PT Field Goal Left Side Center(LC) Mid-Range 16-24 ft. 1610612747 Los Angeles Lakers 2000/10/31 LAL @ POR POR 4 Jump Shot Jump Shot 43 20000012 33.8693 138 175 -118.1318 6 1 ... 0.0 2PT Field Goal Right Side Center(RC) Mid-Range 16-24 ft. 1610612747 Los Angeles Lakers 2000/10/31 LAL @ POR POR 5 Driving Dunk Shot Dunk 155 20000012 34.0443 0 0 -118.2698 6 2 ... 1.0 2PT Field Goal Center(C) Restricted Area Less Than 8 ft. 1610612747 Los Angeles Lakers 2000/10/31 LAL @ POR POR
5 rows × 24 columns
pd. Series( list ( list ( zip ( * ( pd. Series( list ( zip ( df[ 'game_id' ] , df[ 'opponent' ] ) ) )
. unique( ) ) . tolist( ) ) ) [ 1 ] ) ) . value_counts( ) . index[ 0 ]
'SAS'
pd. Series( list ( list ( zip ( * ( pd. Series( list ( zip ( df[ 'game_id' ] , df[ 'opponent' ] ) ) ) . unique( ) ) . tolist( ) ) ) [ 1 ] ) )
0 POR
1 UTA
2 VAN
3 LAC
4 HOU
...
1554 IND
1555 IND
1556 IND
1557 IND
1558 IND
Length: 1559, dtype: object
( df. drop_duplicates( [ "game_id" ] ) [ 'opponent' ] ) . value_counts( ) . idxmax( )
'SAS'
df. drop_duplicates( [ "game_id" ] )
action_type combined_shot_type game_event_id game_id lat loc_x loc_y lon minutes_remaining period ... shot_made_flag shot_type shot_zone_area shot_zone_basic shot_zone_range team_id team_name game_date matchup opponent shot_id 1 Jump Shot Jump Shot 10 20000012 33.9723 167 72 -118.1028 10 1 ... NaN 2PT Field Goal Right Side(R) Mid-Range 16-24 ft. 1610612747 Los Angeles Lakers 2000/10/31 LAL @ POR POR 12 Jump Shot Jump Shot 4 20000019 33.9173 121 127 -118.1488 11 1 ... 1.0 2PT Field Goal Right Side Center(RC) Mid-Range 16-24 ft. 1610612747 Los Angeles Lakers 2000/11/1 LAL vs. UTA UTA 33 Jump Shot Jump Shot 4 20000047 33.9683 163 76 -118.1068 11 1 ... NaN 2PT Field Goal Right Side(R) Mid-Range 16-24 ft. 1610612747 Los Angeles Lakers 2000/11/4 LAL @ VAN VAN 53 Jump Shot Jump Shot 7 20000049 33.9743 24 70 -118.2458 10 1 ... 0.0 2PT Field Goal Center(C) In The Paint (Non-RA) Less Than 8 ft. 1610612747 Los Angeles Lakers 2000/11/5 LAL vs. LAC LAC 67 Jump Shot Jump Shot 22 20000058 34.0083 -119 36 -118.3888 9 1 ... NaN 2PT Field Goal Left Side(L) Mid-Range 8-16 ft. 1610612747 Los Angeles Lakers 2000/11/7 LAL @ HOU HOU ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 30608 Jump Shot Jump Shot 8 49900083 34.0753 115 -31 -118.1548 10 1 ... 1.0 2PT Field Goal Right Side(R) Mid-Range 8-16 ft. 1610612747 Los Angeles Lakers 2000/6/7 LAL vs. IND IND 30621 Jump Shot Jump Shot 4 49900084 33.9813 138 63 -118.1318 11 1 ... 0.0 2PT Field Goal Right Side(R) Mid-Range 8-16 ft. 1610612747 Los Angeles Lakers 2000/6/9 LAL vs. IND IND 30624 Jump Shot Jump Shot 12 49900086 33.8993 29 145 -118.2408 10 1 ... 0.0 2PT Field Goal Center(C) Mid-Range 8-16 ft. 1610612747 Los Angeles Lakers 2000/6/14 LAL @ IND IND 30651 Jump Shot Jump Shot 36 49900087 33.9703 143 74 -118.1268 7 1 ... 0.0 2PT Field Goal Right Side(R) Mid-Range 16-24 ft. 1610612747 Los Angeles Lakers 2000/6/16 LAL @ IND IND 30671 Running Jump Shot Jump Shot 15 49900088 34.0283 -74 16 -118.3438 9 1 ... 0.0 2PT Field Goal Center(C) In The Paint (Non-RA) Less Than 8 ft. 1610612747 Los Angeles Lakers 2000/6/19 LAL vs. IND IND
1559 rows × 24 columns