pandas (1)

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()
SchoolClassIDGenderAddressHeightWeightMathPhysics
0S_1C_11101Mstreet_11736334.0A+
1S_1C_11102Fstreet_21927332.5B+
2S_1C_11103Mstreet_21868287.2B+
3S_1C_11104Fstreet_21678180.4B-
4S_1C_11105Fstreet_41596484.8B+
df = pd.read_csv(r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\table.csv")
df.head()
SchoolClassIDGenderAddressHeightWeightMathPhysics
0S_1C_11101Mstreet_11736334.0A+
1S_1C_11102Fstreet_21927332.5B+
2S_1C_11103Mstreet_21868287.2B+
3S_1C_11104Fstreet_21678180.4B-
4S_1C_11105Fstreet_41596484.8B+
df_txt = pd.read_table(r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\table.txt")
df_txt
col1col2col3col4
02a1.4apple
13b3.4banana
26c2.5orange
35d3.2lemon
df_excel = pd.read_excel(r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\table.xlsx")
df_excel.head()
SchoolClassIDGenderAddressHeightWeightMathPhysics
0S_1C_11101Mstreet_11736334.0A+
1S_1C_11102Fstreet_21927332.5B+
2S_1C_11103Mstreet_21868287.2B+
3S_1C_11104Fstreet_21678180.4B-
4S_1C_11105Fstreet_41596484.8B+
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()
SchoolClassIDGenderAddressHeightWeightMathPhysics
0S_1C_11101Mstreet_11736334.0A+
1S_1C_11102Fstreet_21927332.5B+
2S_1C_11103Mstreet_21868287.2B+
3S_1C_11104Fstreet_21678180.4B-
4S_1C_11105Fstreet_41596484.8B+
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 DateSeasonEpisodeEpisode TitleNameSentence
02011/4/17Season 1Episode 1Winter is Comingwaymar royceWhat do you expect? They're savages. One lot s...
12011/4/17Season 1Episode 1Winter is ComingwillI've never seen wildlings do a thing like this...
22011/4/17Season 1Episode 1Winter is Comingwaymar royceHow close did you get?
32011/4/17Season 1Episode 1Winter is ComingwillClose as any man would.
42011/4/17Season 1Episode 1Winter is CominggaredWe 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 DateSeasonEpisodeEpisode TitleNameSentenceWords
2762011/4/17Season 1Episode 1Winter is Cominga voiceIt's Maester Luwin, my lord.5
30122011/6/19Season 1Episode 10Fire and Bloodaddam marbrandls it true about Stannis and Renly?7
30172011/6/19Season 1Episode 10Fire and Bloodaddam marbrandKevan Lannister2
136102014/6/8Season 4Episode 9The Watchers on the WallaemonAnd what is it that couldn't wait until mornin...10
136142014/6/8Season 4Episode 9The Watchers on the WallaemonOh, 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_typecombined_shot_typegame_event_idgame_idlatloc_xloc_ylonminutes_remainingperiod...shot_made_flagshot_typeshot_zone_areashot_zone_basicshot_zone_rangeteam_idteam_namegame_datematchupopponent
shot_id
1Jump ShotJump Shot102000001233.972316772-118.1028101...NaN2PT Field GoalRight Side(R)Mid-Range16-24 ft.1610612747Los Angeles Lakers2000/10/31LAL @ PORPOR
2Jump ShotJump Shot122000001234.0443-1570-118.4268101...0.02PT Field GoalLeft Side(L)Mid-Range8-16 ft.1610612747Los Angeles Lakers2000/10/31LAL @ PORPOR
3Jump ShotJump Shot352000001233.9093-101135-118.370871...1.02PT Field GoalLeft Side Center(LC)Mid-Range16-24 ft.1610612747Los Angeles Lakers2000/10/31LAL @ PORPOR
4Jump ShotJump Shot432000001233.8693138175-118.131861...0.02PT Field GoalRight Side Center(RC)Mid-Range16-24 ft.1610612747Los Angeles Lakers2000/10/31LAL @ PORPOR
5Driving Dunk ShotDunk1552000001234.044300-118.269862...1.02PT Field GoalCenter(C)Restricted AreaLess Than 8 ft.1610612747Los Angeles Lakers2000/10/31LAL @ PORPOR

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_typecombined_shot_typegame_event_idgame_idlatloc_xloc_ylonminutes_remainingperiod...shot_made_flagshot_typeshot_zone_areashot_zone_basicshot_zone_rangeteam_idteam_namegame_datematchupopponent
shot_id
1Jump ShotJump Shot102000001233.972316772-118.1028101...NaN2PT Field GoalRight Side(R)Mid-Range16-24 ft.1610612747Los Angeles Lakers2000/10/31LAL @ PORPOR
12Jump ShotJump Shot42000001933.9173121127-118.1488111...1.02PT Field GoalRight Side Center(RC)Mid-Range16-24 ft.1610612747Los Angeles Lakers2000/11/1LAL vs. UTAUTA
33Jump ShotJump Shot42000004733.968316376-118.1068111...NaN2PT Field GoalRight Side(R)Mid-Range16-24 ft.1610612747Los Angeles Lakers2000/11/4LAL @ VANVAN
53Jump ShotJump Shot72000004933.97432470-118.2458101...0.02PT Field GoalCenter(C)In The Paint (Non-RA)Less Than 8 ft.1610612747Los Angeles Lakers2000/11/5LAL vs. LACLAC
67Jump ShotJump Shot222000005834.0083-11936-118.388891...NaN2PT Field GoalLeft Side(L)Mid-Range8-16 ft.1610612747Los Angeles Lakers2000/11/7LAL @ HOUHOU
..................................................................
30608Jump ShotJump Shot84990008334.0753115-31-118.1548101...1.02PT Field GoalRight Side(R)Mid-Range8-16 ft.1610612747Los Angeles Lakers2000/6/7LAL vs. INDIND
30621Jump ShotJump Shot44990008433.981313863-118.1318111...0.02PT Field GoalRight Side(R)Mid-Range8-16 ft.1610612747Los Angeles Lakers2000/6/9LAL vs. INDIND
30624Jump ShotJump Shot124990008633.899329145-118.2408101...0.02PT Field GoalCenter(C)Mid-Range8-16 ft.1610612747Los Angeles Lakers2000/6/14LAL @ INDIND
30651Jump ShotJump Shot364990008733.970314374-118.126871...0.02PT Field GoalRight Side(R)Mid-Range16-24 ft.1610612747Los Angeles Lakers2000/6/16LAL @ INDIND
30671Running Jump ShotJump Shot154990008834.0283-7416-118.343891...0.02PT Field GoalCenter(C)In The Paint (Non-RA)Less Than 8 ft.1610612747Los Angeles Lakers2000/6/19LAL vs. INDIND

1559 rows × 24 columns


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值