panda udacity notes

from pandas import Series, DataFrame
d = {'name':Series(['Braud','Cummings','Heikkinen','Allen'], index = ['a', 'b', 'c', 'd']),\
    'age': Series([22, 38, 26, 35], index = ['a', 'b', 'c', 'd']),\
    'fare': Series([7.25, 71.83, 8.05], index = ['a', 'b', 'd']),\
    'survived?': Series([False, True, True, False], index = ['a', 'b', 'c', 'd'])}

df = DataFrame(d)

df['name']
Out[12]: 
a        Braud
b     Cummings
c    Heikkinen
d        Allen
Name: name, dtype: object

df.loc['a']
Out[14]: 
age             22
fare          7.25
name         Braud
survived?    False
Name: a, dtype: object

df[df['age'] >= 30]
Out[15]: 
   age   fare      name  survived?
b   38  71.83  Cummings       True
d   35   8.05     Allen      False
d2 = {'one': Series([1,2,3], index = ['a', 'b', 'c']),\
    'two': Series([1,2,3,4], index = ['a', 'b', 'c', 'd'])}
df2 = DataFrame(d2)
df2.apply(numpy.mean)

Out[17]: 
one    2.0
two    2.5
dtype: float64
df2['one'].map(lambda x: x>1)
Out[18]: 
a    False
b     True
c     True
d    False
Name: one, dtype: bool

df2.applymap(lambda x: x>1)
Out[19]: 
     one    two
a  False  False
b   True   True
c   True   True
d  False   True


Difference between Python and numpy(Numerical Python)

python2 使用 & |
python3 使用and or

import pandas as pd
countries = [

    'Afghanistan', 'Albania', 'Algeria', 'Angola', 'Argentina',

    'Armenia', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas',

    'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium',

    'Belize', 'Benin', 'Bhutan', 'Bolivia',

    'Bosnia and Herzegovina'

]

employment_values = [

    55.70000076,  51.40000153,  50.5       ,  75.69999695,

    58.40000153,  40.09999847,  61.5       ,  57.09999847,

    60.90000153,  66.59999847,  60.40000153,  68.09999847,

    66.90000153,  53.40000153,  48.59999847,  56.79999924,

    71.59999847,  58.40000153,  70.40000153,  41.20000076

]

# Employment data in 2007 for 20 countries

employment = pd.Series(employment_values, index=countries)

def max_employment(employment):

    '''

    Fill in this function to return the name of the country

    with the highest employment in the given employment

    data, and the employment in that country.

    

    The input will be a Pandas series where the values

    are employment and the index is country names.

    

    Try using the Pandas argmax() function. Documention is

    here: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.argmax.html

    '''

    max_country_index = employment.argmax(axis = employment_values)

    max_country = max_country_index      # Replace this with your code

    max_value = employment[max_country_index]   # Replace this with your code

    print max_country, max_value

max_employment(employment)

Angola 75.69999695

pandas dataframe exercise

# Subway ridership for 5 stations on 10 different days
ridership_df = pd.DataFrame(
    data=[[   0,    0,    2,    5,    0],
          [1478, 3877, 3674, 2328, 2539],
          [1613, 4088, 3991, 6461, 2691],
          [1560, 3392, 3826, 4787, 2613],
          [1608, 4802, 3932, 4477, 2705],
          [1576, 3933, 3909, 4979, 2685],
          [  95,  229,  255,  496,  201],
          [   2,    0,    1,   27,    0],
          [1438, 3785, 3589, 4174, 2215],
          [1342, 4043, 4009, 4665, 3033]],
    index=['05-01-11', '05-02-11', '05-03-11', '05-04-11', '05-05-11',
           '05-06-11', '05-07-11', '05-08-11', '05-09-11', '05-10-11'],
    columns=['R003', 'R004', 'R005', 'R006', 'R007']
)

# Change False to True for each block of code to see what it does

# DataFrame creation
if False:
    # You can create a DataFrame out of a dictionary mapping column names to values
    df_1 = pd.DataFrame({'A': [0, 1, 2], 'B': [3, 4, 5]})
    print df_1

    # You can also use a list of lists or a 2D NumPy array
    df_2 = pd.DataFrame([[0, 1, 2], [3, 4, 5]], columns=['A', 'B', 'C'])
    print df_2
   

# Accessing elements
if False:
    print ridership_df.iloc[0]
    print ridership_df.loc['05-05-11']
    print ridership_df['R003']
    print ridership_df.iloc[1, 3]
    
# Accessing multiple rows
if False:
    print ridership_df.iloc[1:4]
    
# Accessing multiple columns
if False:
    print ridership_df[['R003', 'R005']]
    
# Pandas axis
if False:
    df = pd.DataFrame({'A': [0, 1, 2], 'B': [3, 4, 5]})
    print df.sum()
    print df.sum(axis=1)
    print df.values.sum()
    
def mean_riders_for_max_station(ridership):
    '''
    Fill in this function to find the station with the maximum riders on the
    first day, then return the mean riders per day for that station. Also
    return the mean ridership overall for comparsion.
    
    This is the same as a previous exercise, but this time the
    input is a Pandas DataFrame rather than a 2D NumPy array.
    '''
    overall_mean = None # Replace this with your code
    mean_for_max = None # Replace this with your code
    
    return (overall_mean, mean_for_max)

Summing up, apply works on a row / column basis of a DataFrame, applymap works element-wise on a DataFrame, and map works element-wise on a Series.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末灌灾,一起剝皮案震驚了整個(gè)濱河市宣渗,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌胚膊,老刑警劉巖肛宋,帶你破解...
    沈念sama閱讀 211,042評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件该酗,死亡現(xiàn)場(chǎng)離奇詭異揉阎,居然都是意外死亡荆秦,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,996評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門癌刽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來役首,“玉大人尝丐,你說我怎么就攤上這事『獍拢” “怎么了爹袁?”我有些...
    開封第一講書人閱讀 156,674評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)矮固。 經(jīng)常有香客問我失息,道長(zhǎng),這世上最難降的妖魔是什么档址? 我笑而不...
    開封第一講書人閱讀 56,340評(píng)論 1 283
  • 正文 為了忘掉前任盹兢,我火速辦了婚禮,結(jié)果婚禮上守伸,老公的妹妹穿的比我還像新娘绎秒。我一直安慰自己,他們只是感情好尼摹,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,404評(píng)論 5 384
  • 文/花漫 我一把揭開白布见芹。 她就那樣靜靜地躺著,像睡著了一般窘问。 火紅的嫁衣襯著肌膚如雪辆童。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,749評(píng)論 1 289
  • 那天惠赫,我揣著相機(jī)與錄音把鉴,去河邊找鬼。 笑死儿咱,一個(gè)胖子當(dāng)著我的面吹牛庭砍,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播混埠,決...
    沈念sama閱讀 38,902評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼怠缸,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了钳宪?” 一聲冷哼從身側(cè)響起揭北,我...
    開封第一講書人閱讀 37,662評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎吏颖,沒想到半個(gè)月后搔体,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,110評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡半醉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評(píng)論 2 325
  • 正文 我和宋清朗相戀三年疚俱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片缩多。...
    茶點(diǎn)故事閱讀 38,577評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡呆奕,死狀恐怖养晋,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情梁钾,我是刑警寧澤绳泉,帶...
    沈念sama閱讀 34,258評(píng)論 4 328
  • 正文 年R本政府宣布,位于F島的核電站陈轿,受9級(jí)特大地震影響圈纺,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜麦射,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,848評(píng)論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望灯谣。 院中可真熱鬧潜秋,春花似錦、人聲如沸胎许。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,726評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽辜窑。三九已至钩述,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間穆碎,已是汗流浹背牙勘。 一陣腳步聲響...
    開封第一講書人閱讀 31,952評(píng)論 1 264
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留所禀,地道東北人方面。 一個(gè)月前我還...
    沈念sama閱讀 46,271評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像色徘,于是被迫代替她去往敵國(guó)和親恭金。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,452評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,429評(píng)論 0 23
  • 有一個(gè)遙遠(yuǎn)的國(guó)度褂策,快樂王國(guó)横腿,物產(chǎn)豐饒,人民安居樂業(yè)斤寂,幸腹⒑福快樂。忽然有一年扬蕊,發(fā)生一場(chǎng)大災(zāi)難搀别,有一個(gè)巫婆來到這個(gè)國(guó)家,...
    維揚(yáng)之水閱讀 834評(píng)論 3 3
  • 文/友不舊 早在幾年前尾抑,讀三生三世小說的時(shí)候歇父,本是對(duì)離鏡沒什么大印象的蒂培。大概是劇里的演員演技顏值皆在, 實(shí)在不能當(dāng)...
    友不舊閱讀 603評(píng)論 2 2
  • 《晨起》 寒冰 晨起倚欄獨(dú)望天 輕風(fēng)拂面納秋涼 耳聆風(fēng)語鳥聲鳴 難得此景享清閑 凡世俗囂渾不見 心...
    eceff7a5c042寒冰閱讀 174評(píng)論 0 3