Pandas包函数快速查找手册

使用python做数据分析最关键的库之一pandas,在数据处理最为常用,pandas中的函数分为如下几大类

1. 输入输出类

读入数据类

  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

输出数据类函数

  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. 生成测试数据

  1. pd.DataFrame(np.random.rand(20,5)) -生成一个20行5列的随机浮点数数据框
  2. pd.Series(my_list) -由一个可迭代的my_list生成一个Series
  3. df.index = pd.date_range('1900/1/30',periods = df.shape[0]) -增加一个时间序列的index

3. 查看数据总体情况

  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) -查看唯一的值并计数
  7. df.apply(pd.Series.value_couonts) - 对所有列唯一值计数

4. 数据选取

  1. df[col] 作为Series返回col列
  2. df[[col1, col2]] 返回多列数据,作为新数据框返回
  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. 数据清洗

  1. df.columns = ['a','b','c']- 重命名列名
  2. pd.isnull() - 检查空值,返回布尔值数组
  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个数大于你的行 /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的数据类型转换为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) - 对列进行大规模重命名 /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) - 大规模更改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) - 创建一个透视表,根据col1分组,计算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. 统计函数

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. 连接数据

  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'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,451评论 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,322评论 0 10
  • pandas模塊 基本属性 df.dtypes: data type of columns列...
    wong11阅读 1,283评论 0 2
  • 日精进,今日体验:今天活不多,在维修每一辆车都有不同的客户,有好说的。有不好说的。认真对待每一位客户。 赞 踩 小...
    隆非凡阅读 81评论 0 0
  • 从今天开始,每天写一篇日记,百儿八十的字,记录一下自己兢兢业业亦或是碌碌无为的生活。 希望微信的朋友圈一直活下去,...
    内心很帅在巴黎阅读 2,579评论 48 74