Pandas包函數(shù)快速查找手冊(cè)

使用python做數(shù)據(jù)分析最關(guān)鍵的庫(kù)之一pandas侈咕,在數(shù)據(jù)處理最為常用磕潮,pandas中的函數(shù)分為如下幾大類

1. 輸入輸出類

讀入數(shù)據(jù)類

  1. pd.read_csv(filename) -from a CSV file
  2. pd.read_excel(filename) -from a excel file
  3. pd.read_sql(query, connection_object) -Reads from a SQL table/database
  4. pd.read_json(json_string) - Reads from a JSON formatted string, URL or file.
  5. pd.read_html(url) - Parses an html URL, string or file and extracts tables to a list of dataframes
  6. pd.read_clipboard()- Takes the contents of your clipboard and passes it to read_table()
  7. pd.DataFrame(dict) -from a dict,keys for columns name, values for data as lists

輸出數(shù)據(jù)類函數(shù)

  1. df.to _excel(filename) - Writes to an Excel file
  2. df.to_csv(filename) Writes to a CSV file
  3. df.to_sql(table_name, connection_object) -writes to a SQL table
  4. df.to_json(filename) - Writes to a file in JSON format
  5. df.to_html(filename)- Saves as an HTML table
  6. df.to_clipboard() Writes to the clipboard

2. 生成測(cè)試數(shù)據(jù)

  1. pd.DataFrame(np.random.rand(20,5)) -生成一個(gè)20行5列的隨機(jī)浮點(diǎn)數(shù)數(shù)據(jù)框
  2. pd.Series(my_list) -由一個(gè)可迭代的my_list生成一個(gè)Series
  3. df.index = pd.date_range('1900/1/30',periods = df.shape[0]) -增加一個(gè)時(shí)間序列的index

3. 查看數(shù)據(jù)總體情況

  1. df.head(n)
  2. df.tail(n)
  3. df.shape() number of rows and columns
  4. df.info()- Index, Datatype and Memory information
  5. df.describe()- Summary statistics for numerical columns
  6. s.value_counts(dropna = False) -查看唯一的值并計(jì)數(shù)
  7. df.apply(pd.Series.value_couonts) - 對(duì)所有列唯一值計(jì)數(shù)

4. 數(shù)據(jù)選取

  1. df[col] 作為Series返回col列
  2. df[[col1, col2]] 返回多列數(shù)據(jù)翠胰,作為新數(shù)據(jù)框返回
  3. s.iloc[0]- Selection by position
  4. s.loc[0]- Selection by index
  5. df.iloc[0,:] - First row
  6. df.iloc[0,0]- First element of first column

5. 數(shù)據(jù)清洗

  1. df.columns = ['a','b','c']- 重命名列名
  2. pd.isnull() - 檢查空值容贝,返回布爾值數(shù)組
  3. pd.notnull() - Opposite of s.isnull()
  4. df.dropna()-刪除所有包含NA值的行 Drops all rows that contain null values
  5. df.dropna(axis=1) - 刪除所有包含NA的列Drops all columns that contain null values
  6. df.dropna(axis=1,thresh=n) - 刪除所有行中NA個(gè)數(shù)大于你的行 /Drops all rows have less than n non null values
  7. df.fillna(x) - 用X填充NA /Replaces all null values with x
  8. s.fillna(s.mean()) - 用均值填充NA /Replaces all null values with the mean (mean can be replaced with almost any function from the statistics section)
  9. s.astype(float) -將Series的數(shù)據(jù)類型轉(zhuǎn)換為float / Converts the datatype of the series to float
  10. s.replace(1,'one') - 用'one'代替1 /Replaces all values equal to 1 with 'one'
  11. s.replace([1,3],['one','three']) - Replaces all 1 with 'one' and 3 with 'three'
  12. df.rename(columns=lambda x: x + 1) - 對(duì)列進(jìn)行大規(guī)模重命名 /Mass renaming of columns
  13. df.rename(columns={'old_name': 'new_ name'}) - 選擇性重命名列名 /Selective renaming
  14. df.set_index('column_one') - 更改index /Changes the index
  15. df.rename(index=lambda x: x + 1) - 大規(guī)模更改index
    /Mass renaming of index

6. 過濾、排序和分組

  1. df[df[col] > 0.5] - Rows where the col column is greater than 0.5
  2. df[(df[col] > 0.5) & (df[col] < 0.7)] - Rows where 0.7 > col > 0.5
  3. df.sort_values(col1) -按col1升序排序 Sorts values by col1 in ascending order
  4. df.sort_values(col2,ascending=False) -按col2降序排序 Sorts values by col2 in descending order
  5. df.sort_values([col1,col2], ascending=[True,False]) - Sorts values by col1 in ascending order then col2 in descending order
  6. df.groupby(col) - Returns a groupby object for values from one column
  7. df.groupby([col1,col2]) - Returns a groupby object values from multiple columns
  8. df.groupby(col1)[col2].mean()/df.groupby(col1).mean()[col2] - Returns the mean of the values in col2, grouped by the values in col1 (mean can be replaced with almost any function from the statistics section)
  9. df.pivot_table(index=col1,values= [col2,col3],aggfunc=mean) - 創(chuàng)建一個(gè)透視表之景,根據(jù)col1分組斤富,計(jì)算col2,col3的均值 /Creates a pivot table that groups by col1 and calculates the mean of col2 and col3
  10. df.groupby(col1).agg(np.mean) - Finds the average across all columns for every unique column 1 group
  11. df.apply(np.mean) - Applies a function across each column
  12. df.apply(np.max, axis=1) - Applies a function across each row

7. 統(tǒng)計(jì)函數(shù)

These can all be applied to a series as well.

  1. df.describe() - Summary statistics for numerical columns
  2. df.mean() - Returns the mean of all columns
  3. df.corr() - Returns the correlation between columns in a DataFrame
  4. df.count() - Returns the number of non-null values in each DataFrame column
  5. df.max() - Returns the highest value in each column
  6. df.min() - Returns the lowest value in each column
  7. df.median() - Returns the median of each column
  8. df.std() - Returns the standard deviation of each column

8. 連接數(shù)據(jù)

  1. df1.append(df2) - Adds the rows in df1 to the end of df2 (columns should be identical)
  2. pd.concat([df1, df2],axis=1) - Adds the columns in df1 to the end of df2 (rows should be identical)
  3. df1.join(df2,on=col1,how='inner') - SQL-style joins the columns in df1 with the columns on df2 where the rows for col have identical values. how can be one of 'left', 'right', 'outer', 'inner'
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市锻狗,隨后出現(xiàn)的幾起案子满力,更是在濱河造成了極大的恐慌,老刑警劉巖轻纪,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件油额,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡桐磁,警方通過查閱死者的電腦和手機(jī)悔耘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門讲岁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來我擂,“玉大人,你說我怎么就攤上這事缓艳⌒DΓ” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵阶淘,是天一觀的道長(zhǎng)衙吩。 經(jīng)常有香客問我,道長(zhǎng)溪窒,這世上最難降的妖魔是什么坤塞? 我笑而不...
    開封第一講書人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮澈蚌,結(jié)果婚禮上摹芙,老公的妹妹穿的比我還像新娘。我一直安慰自己宛瞄,他們只是感情好浮禾,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著份汗,像睡著了一般盈电。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上杯活,一...
    開封第一講書人閱讀 51,578評(píng)論 1 305
  • 那天匆帚,我揣著相機(jī)與錄音,去河邊找鬼旁钧。 笑死卷扮,一個(gè)胖子當(dāng)著我的面吹牛荡澎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播晤锹,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼摩幔,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了鞭铆?” 一聲冷哼從身側(cè)響起或衡,我...
    開封第一講書人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎车遂,沒想到半個(gè)月后封断,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡舶担,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年坡疼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片衣陶。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡柄瑰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出剪况,到底是詐尸還是另有隱情教沾,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布译断,位于F島的核電站授翻,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏孙咪。R本人自食惡果不足惜堪唐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望翎蹈。 院中可真熱鬧淮菠,春花似錦、人聲如沸杨蛋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)逞力。三九已至曙寡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間寇荧,已是汗流浹背举庶。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留揩抡,地道東北人户侥。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓镀琉,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親蕊唐。 傳聞我的和親對(duì)象是個(gè)殘疾皇子屋摔,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

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

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,456評(píng)論 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,332評(píng)論 0 10
  • pandas模塊 基本屬性 df.dtypes: data type of columns列...
    wong11閱讀 1,285評(píng)論 0 2
  • 日精進(jìn),今日體驗(yàn):今天活不多替梨,在維修每一輛車都有不同的客戶钓试,有好說的。有不好說的副瀑。認(rèn)真對(duì)待每一位客戶弓熏。 贊 踩 小...
    隆非凡閱讀 81評(píng)論 0 0
  • 從今天開始,每天寫一篇日記糠睡,百兒八十的字挽鞠,記錄一下自己兢兢業(yè)業(yè)亦或是碌碌無為的生活。 希望微信的朋友圈一直活下去狈孔,...
    內(nèi)心很帥在巴黎閱讀 2,579評(píng)論 48 74