pandas 基礎(chǔ)入門 DataFrame的創(chuàng)建


數(shù)據(jù)框(dataFrame)的創(chuàng)建:

import numpy as np
import pandas as pd
data = {'year':[2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
         'team':['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions',
                     'Lions', 'Lions'],
         'wins':[11, 8, 10, 15, 11, 6, 10, 4],
         'losses':[5, 8, 6, 1, 5, 10, 6, 12]
}
football =pd.DataFrame(data)
print football

輸出:

   losses     team  wins  year
0       5    Bears    11  2010
1       8    Bears     8  2011
2       6    Bears    10  2012
3       1  Packers    15  2011
4       5  Packers    11  2012
5      10    Lions     6  2010
6       6    Lions    10  2011
7      12    Lions     4  2012
Pandas 也有很多幫助你理解數(shù)據(jù)框中一些基本信息的方法:
  • dtypes: 獲取每一柱數(shù)據(jù)的數(shù)據(jù)類型
  • describle: 對于用來觀察數(shù)據(jù)框的數(shù)值列的基本是有數(shù)據(jù)用的
  • head :顯示前5行數(shù)據(jù)集
  • tail : 顯示最后5行的數(shù)據(jù)集

test:

data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
            'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions',
                     'Lions', 'Lions'],
            'wins': [11, 8, 10, 15, 11, 6, 10, 4],
            'losses': [5, 8, 6, 1, 5, 10, 6, 12]}
    football = pd.DataFrame(data)
    print football.dtypes
    print ""
    print football.describe()
    print ""
    print football.head()
    print ""
    print football.tail()

運行結(jié)果:

# 數(shù)據(jù)類型
losses     int64
team      object
wins       int64
year       int64
dtype: object

#返回一些基本數(shù)據(jù)
          losses       wins         year
count   8.000000   8.000000     8.000000  # 總數(shù)
mean    6.625000   9.375000  2011.125000  #平均數(shù)
std     3.377975   3.377975     0.834523  #標(biāo)準(zhǔn)差
min     1.000000   4.000000  2010.000000 #最小值
25%     5.000000   7.500000  2010.750000
50%     6.000000  10.000000  2011.000000
75%     8.500000  11.000000  2012.000000
max    12.000000  15.000000  2012.000000#最大值

   losses     team  wins  year
0       5    Bears    11  2010
1       8    Bears     8  2011
2       6    Bears    10  2012
3       1  Packers    15  2011
4       5  Packers    11  2012

   losses     team  wins  year
3       1  Packers    15  2011
4       5  Packers    11  2012
5      10    Lions     6  2010
6       6    Lions    10  2011
7      12    Lions     4  2012

再來一組code:

from pandas import DataFrame, Series

#################
# Syntax Reminder:
#
# The following code would create a two-column pandas DataFrame
# named df with columns labeled 'name' and 'age':
#
# people = ['Sarah', 'Mike', 'Chrisna']
# ages  =  [28, 32, 25]
# df = DataFrame({'name' : Series(people),
#                 'age'  : Series(ages)})

def create_dataframe():
    '''
    Create a pandas dataframe called 'olympic_medal_counts_df' containing
    the data from the table of 2014 Sochi winter olympics medal counts.  

    The columns for this dataframe should be called 
    'country_name', 'gold', 'silver', and 'bronze'.  

    There is no need to  specify row indexes for this dataframe 
    (in this case, the rows will automatically be assigned numbered indexes).
    
    You do not need to call the function in your code when running it in the
    browser - the grader will do that automatically when you submit or test it.
    '''

    countries = ['Russian Fed.', 'Norway', 'Canada', 'United States',
                 'Netherlands', 'Germany', 'Switzerland', 'Belarus',
                 'Austria', 'France', 'Poland', 'China', 'Korea', 
                 'Sweden', 'Czech Republic', 'Slovenia', 'Japan',
                 'Finland', 'Great Britain', 'Ukraine', 'Slovakia',
                 'Italy', 'Latvia', 'Australia', 'Croatia', 'Kazakhstan']

    gold = [13, 11, 10, 9, 8, 8, 6, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
    silver = [11, 5, 10, 7, 7, 6, 3, 0, 8, 4, 1, 4, 3, 7, 4, 2, 4, 3, 1, 0, 0, 2, 2, 2, 1, 0]
    bronze = [9, 10, 5, 12, 9, 5, 2, 1, 5, 7, 1, 2, 2, 6, 2, 4, 3, 1, 2, 1, 0, 6, 2, 1, 0, 1]

  
    data ={
        'countries':countries,
        'gold':gold,
        'silver':silver,
        'bronze':bronze}
        
    olympic_medal_counts_df=DataFrame(data)
    return olympic_medal_counts_df

運行結(jié)果:

   bronze       countries  gold  silver
0        9    Russian Fed.    13      11
1       10          Norway    11       5
2        5          Canada    10      10
3       12   United States     9       7
4        9     Netherlands     8       7
5        5         Germany     8       6
6        2     Switzerland     6       3
7        1         Belarus     5       0
8        5         Austria     4       8
9        7          France     4       4
10       1          Poland     4       1
11       2           China     3       4
12       2           Korea     3       3
13       6          Sweden     2       7
14       2  Czech Republic     2       4
15       4        Slovenia     2       2
16       3           Japan     1       4
17       1         Finland     1       3
18       2   Great Britain     1       1
19       1         Ukraine     1       0
20       0        Slovakia     1       0
21       6           Italy     0       2
22       2          Latvia     0       2
23       1       Australia     0       2
24       0         Croatia     0       1
25       1      Kazakhstan     0       0
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末肆资,一起剝皮案震驚了整個濱河市擦盾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌拉盾,老刑警劉巖磅网,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件淋袖,死亡現(xiàn)場離奇詭異摊聋,居然都是意外死亡晦炊,警方通過查閱死者的電腦和手機苦囱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進(jìn)店門嗅绸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人沿彭,你說我怎么就攤上這事朽砰。” “怎么了喉刘?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵瞧柔,是天一觀的道長。 經(jīng)常有香客問我睦裳,道長造锅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任廉邑,我火速辦了婚禮哥蔚,結(jié)果婚禮上倒谷,老公的妹妹穿的比我還像新娘。我一直安慰自己糙箍,他們只是感情好渤愁,可當(dāng)我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著深夯,像睡著了一般抖格。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上咕晋,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天雹拄,我揣著相機與錄音,去河邊找鬼掌呜。 笑死滓玖,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的质蕉。 我是一名探鬼主播势篡,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼饰剥!你這毒婦竟也來了殊霞?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤汰蓉,失蹤者是張志新(化名)和其女友劉穎绷蹲,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體顾孽,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡祝钢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了若厚。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拦英。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖测秸,靈堂內(nèi)的尸體忽然破棺而出疤估,到底是詐尸還是另有隱情,我是刑警寧澤霎冯,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布铃拇,位于F島的核電站,受9級特大地震影響沈撞,放射性物質(zhì)發(fā)生泄漏慷荔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一缠俺、第九天 我趴在偏房一處隱蔽的房頂上張望显晶。 院中可真熱鬧贷岸,春花似錦、人聲如沸磷雇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽唯笙。三九已至户敬,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間睁本,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工忠怖, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留呢堰,地道東北人。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓凡泣,卻偏偏與公主長得像枉疼,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子鞋拟,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,974評論 2 355

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