Pandas中DataFrame基本函数整理(小结)

构造函数

DataFrame([data, index, columns, dtype, copy]) #构造数据框

属性和数据

DataFrame.axes                #index: 行标签;columns: 列标签
DataFrame.as_matrix([columns])        #转换为矩阵
DataFrame.dtypes               #返回数据的类型
DataFrame.ftypes               #返回每一列的 数据类型float64:dense
DataFrame.get_dtype_counts()         #返回数据框数据类型的个数
DataFrame.get_ftype_counts()         #返回数据框数据类型float64:dense的个数
DataFrame.select_dtypes([include, include])  #根据数据类型选取子数据框
DataFrame.values               #Numpy的展示方式
DataFrame.axes                #返回横纵坐标的标签名
DataFrame.ndim                #返回数据框的纬度
DataFrame.size                #返回数据框元素的个数
DataFrame.shape                #返回数据框的形状
DataFrame.memory_usage()           #每一列的存储

类型转换

DataFrame.astype(dtype[, copy, errors])    #转换数据类型
DataFrame.copy([deep])            #deep深度复制数据
DataFrame.isnull()              #以布尔的方式返回空值
DataFrame.notnull()              #以布尔的方式返回非空值

索引和迭代

DataFrame.head([n])              #返回前n行数据
DataFrame.at                 #快速标签常量访问器
DataFrame.iat                 #快速整型常量访问器
DataFrame.loc                 #标签定位,使用名称
DataFrame.iloc                #整型定位,使用数字
DataFrame.insert(loc, column, value)     #在特殊地点loc[数字]插入column[列名]某列数据
DataFrame.iter()               #Iterate over infor axis
DataFrame.iteritems()             #返回列名和序列的迭代器
DataFrame.iterrows()             #返回索引和序列的迭代器
DataFrame.itertuples([index, name])      #Iterate over DataFrame rows as namedtuples, with index value as first element of the tuple.
DataFrame.lookup(row_labels, col_labels)   #Label-based “fancy indexing” function for DataFrame.
DataFrame.pop(item)              #返回删除的项目
DataFrame.tail([n])              #返回最后n行
DataFrame.xs(key[, axis, level, drop_level]) #Returns a cross-section (row(s) or column(s)) from the Series/DataFrame.
DataFrame.isin(values)            #是否包含数据框中的元素
DataFrame.where(cond[, other, inplace, …])  #条件筛选
DataFrame.mask(cond[, other, inplace, …])   #Return an object of same shape as self and whose corresponding entries are from self where cond is False and otherwise are from other.
DataFrame.query(expr[, inplace])       #Query the columns of a frame with a boolean expression.

二元运算

DataFrame.add(other[,axis,fill_value])    #加法,元素指向
DataFrame.sub(other[,axis,fill_value])    #减法,元素指向
DataFrame.mul(other[, axis,fill_value])    #乘法,元素指向
DataFrame.div(other[, axis,fill_value])    #小数除法,元素指向
DataFrame.truediv(other[, axis, level, …])  #真除法,元素指向
DataFrame.floordiv(other[, axis, level, …])  #向下取整除法,元素指向
DataFrame.mod(other[, axis,fill_value])    #模运算,元素指向
DataFrame.pow(other[, axis,fill_value])    #幂运算,元素指向
DataFrame.radd(other[, axis,fill_value])   #右侧加法,元素指向
DataFrame.rsub(other[, axis,fill_value])   #右侧减法,元素指向
DataFrame.rmul(other[, axis,fill_value])   #右侧乘法,元素指向
DataFrame.rdiv(other[, axis,fill_value])   #右侧小数除法,元素指向
DataFrame.rtruediv(other[, axis, …])     #右侧真除法,元素指向
DataFrame.rfloordiv(other[, axis, …])     #右侧向下取整除法,元素指向
DataFrame.rmod(other[, axis,fill_value])   #右侧模运算,元素指向
DataFrame.rpow(other[, axis,fill_value])   #右侧幂运算,元素指向
DataFrame.lt(other[, axis, level])      #类似Array.lt
DataFrame.gt(other[, axis, level])      #类似Array.gt
DataFrame.le(other[, axis, level])      #类似Array.le
DataFrame.ge(other[, axis, level])      #类似Array.ge
DataFrame.ne(other[, axis, level])      #类似Array.ne
DataFrame.eq(other[, axis, level])      #类似Array.eq
DataFrame.combine(other,func[,fill_value, …]) #Add two DataFrame objects and do not propagate NaN values, so if for a
DataFrame.combine_first(other)        #Combine two DataFrame objects and default to non-null values in frame calling the method.

函数应用&分组&窗口

DataFrame.apply(func[, axis, broadcast, …])  #应用函数
DataFrame.applymap(func)           #Apply a function to a DataFrame that is intended to operate elementwise, i.e.
DataFrame.aggregate(func[, axis])       #Aggregate using callable, string, dict, or list of string/callables
DataFrame.transform(func, *args, **kwargs)  #Call function producing a like-indexed NDFrame
DataFrame.groupby([by, axis, level, …])    #分组
DataFrame.rolling(window[, min_periods, …])  #滚动窗口
DataFrame.expanding([min_periods, freq, …])  #拓展窗口
DataFrame.ewm([com, span, halflife, …])   #指数权重窗口

描述统计学

DataFrame.abs()                #返回绝对值
DataFrame.all([axis, bool_only, skipna])   #Return whether all elements are True over requested axis
DataFrame.any([axis, bool_only, skipna])   #Return whether any element is True over requested axis
DataFrame.clip([lower, upper, axis])     #Trim values at input threshold(s).
DataFrame.clip_lower(threshold[, axis])    #Return copy of the input with values below given value(s) truncated.
DataFrame.clip_upper(threshold[, axis])    #Return copy of input with values above given value(s) truncated.
DataFrame.corr([method, min_periods])     #返回本数据框成对列的相关性系数
DataFrame.corrwith(other[, axis, drop])    #返回不同数据框的相关性
DataFrame.count([axis, level, numeric_only]) #返回非空元素的个数
DataFrame.cov([min_periods])         #计算协方差
DataFrame.cummax([axis, skipna])       #Return cumulative max over requested axis.
DataFrame.cummin([axis, skipna])       #Return cumulative minimum over requested axis.
DataFrame.cumprod([axis, skipna])       #返回累积
DataFrame.cumsum([axis, skipna])       #返回累和
DataFrame.describe([percentiles,include, …]) #整体描述数据框
DataFrame.diff([periods, axis])        #1st discrete difference of object
DataFrame.eval(expr[, inplace])        #Evaluate an expression in the context of the calling DataFrame instance.
DataFrame.kurt([axis, skipna, level, …])   #返回无偏峰度Fisher's (kurtosis of normal == 0.0).
DataFrame.mad([axis, skipna, level])     #返回偏差
DataFrame.max([axis, skipna, level, …])    #返回最大值
DataFrame.mean([axis, skipna, level, …])   #返回均值
DataFrame.median([axis, skipna, level, …])  #返回中位数
DataFrame.min([axis, skipna, level, …])    #返回最小值
DataFrame.mode([axis, numeric_only])     #返回众数
DataFrame.pct_change([periods, fill_method]) #返回百分比变化
DataFrame.prod([axis, skipna, level, …])   #返回连乘积
DataFrame.quantile([q, axis, numeric_only])  #返回分位数
DataFrame.rank([axis, method, numeric_only]) #返回数字的排序
DataFrame.round([decimals])          #Round a DataFrame to a variable number of decimal places.
DataFrame.sem([axis, skipna, level, ddof])  #返回无偏标准误
DataFrame.skew([axis, skipna, level, …])   #返回无偏偏度
DataFrame.sum([axis, skipna, level, …])    #求和
DataFrame.std([axis, skipna, level, ddof])  #返回标准误差
DataFrame.var([axis, skipna, level, ddof])  #返回无偏误差 

从新索引&选取&标签操作

DataFrame.add_prefix(prefix)         #添加前缀
DataFrame.add_suffix(suffix)         #添加后缀
DataFrame.align(other[, join, axis, level])  #Align two object on their axes with the
DataFrame.drop(labels[, axis, level, …])   #返回删除的列
DataFrame.drop_duplicates([subset, keep, …]) #Return DataFrame with duplicate rows removed, optionally only
DataFrame.duplicated([subset, keep])     #Return boolean Series denoting duplicate rows, optionally only
DataFrame.equals(other)            #两个数据框是否相同
DataFrame.filter([items, like, regex, axis]) #过滤特定的子数据框
DataFrame.first(offset)            #Convenience method for subsetting initial periods of time series data based on a date offset.
DataFrame.head([n])              #返回前n行
DataFrame.idxmax([axis, skipna])       #Return index of first occurrence of maximum over requested axis.
DataFrame.idxmin([axis, skipna])       #Return index of first occurrence of minimum over requested axis.
DataFrame.last(offset)            #Convenience method for subsetting final periods of time series data based on a date offset.
DataFrame.reindex([index, columns])      #Conform DataFrame to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index.
DataFrame.reindex_axis(labels[, axis, …])   #Conform input object to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index.
DataFrame.reindex_like(other[, method, …])  #Return an object with matching indices to myself.
DataFrame.rename([index, columns])      #Alter axes input function or functions.
DataFrame.rename_axis(mapper[, axis, copy])  #Alter index and / or columns using input function or functions.
DataFrame.reset_index([level, drop, …])    #For DataFrame with multi-level index, return new DataFrame with labeling information in the columns under the index names, defaulting to ‘level_0', ‘level_1', etc.
DataFrame.sample([n, frac, replace, …])    #返回随机抽样
DataFrame.select(crit[, axis])        #Return data corresponding to axis labels matching criteria
DataFrame.set_index(keys[, drop, append ])  #Set the DataFrame index (row labels) using one or more existing columns.
DataFrame.tail([n])              #返回最后几行
DataFrame.take(indices[, axis, convert])   #Analogous to ndarray.take
DataFrame.truncate([before, after, axis ])  #Truncates a sorted NDFrame before and/or after some particular index value.

处理缺失值

DataFrame.dropna([axis, how, thresh, …])   #Return object with labels on given axis omitted where alternately any
DataFrame.fillna([value, method, axis, …])  #填充空值
DataFrame.replace([to_replace, value, …])   #Replace values given in ‘to_replace' with ‘value'.

从新定型&排序&转变形态

DataFrame.pivot([index, columns, values])   #Reshape data (produce a “pivot” table) based on column values.
DataFrame.reorder_levels(order[, axis])    #Rearrange index levels using input order.
DataFrame.sort_values(by[, axis, ascending]) #Sort by the values along either axis
DataFrame.sort_index([axis, level, …])    #Sort object by labels (along an axis)
DataFrame.nlargest(n, columns[, keep])    #Get the rows of a DataFrame sorted by the n largest values of columns.
DataFrame.nsmallest(n, columns[, keep])    #Get the rows of a DataFrame sorted by the n smallest values of columns.
DataFrame.swaplevel([i, j, axis])       #Swap levels i and j in a MultiIndex on a particular axis
DataFrame.stack([level, dropna])       #Pivot a level of the (possibly hierarchical) column labels, returning a DataFrame (or Series in the case of an object with a single level of column labels) having a hierarchical index with a new inner-most level of row labels.
DataFrame.unstack([level, fill_value])    #Pivot a level of the (necessarily hierarchical) index labels, returning a DataFrame having a new level of column labels whose inner-most level consists of the pivoted index labels.
DataFrame.melt([id_vars, value_vars, …])   #“Unpivots” a DataFrame from wide format to long format, optionally
DataFrame.T                  #Transpose index and columns
DataFrame.to_panel()             #Transform long (stacked) format (DataFrame) into wide (3D, Panel) format.
DataFrame.to_xarray()             #Return an xarray object from the pandas object.
DataFrame.transpose(*args, **kwargs)     #Transpose index and columns

Combining& joining&merging

DataFrame.append(other[, ignore_index, …])  #追加数据
DataFrame.assign(**kwargs)          #Assign new columns to a DataFrame, returning a new object (a copy) with all the original columns in addition to the new ones.
DataFrame.join(other[, on, how, lsuffix, …]) #Join columns with other DataFrame either on index or on a key column.
DataFrame.merge(right[, how, on, left_on, …]) #Merge DataFrame objects by performing a database-style join operation by columns or indexes.
DataFrame.update(other[, join, overwrite, …]) #Modify DataFrame in place using non-NA values from passed DataFrame.

时间序列

DataFrame.asfreq(freq[, method, how, …])   #将时间序列转换为特定的频次
DataFrame.asof(where[, subset])        #The last row without any NaN is taken (or the last row without
DataFrame.shift([periods, freq, axis])    #Shift index by desired number of periods with an optional time freq
DataFrame.first_valid_index()         #Return label for first non-NA/null value
DataFrame.last_valid_index()         #Return label for last non-NA/null value
DataFrame.resample(rule[, how, axis, …])   #Convenience method for frequency conversion and resampling of time series.
DataFrame.to_period([freq, axis, copy])    #Convert DataFrame from DatetimeIndex to PeriodIndex with desired
DataFrame.to_timestamp([freq, how, axis])   #Cast to DatetimeIndex of timestamps, at beginning of period
DataFrame.tz_convert(tz[, axis, level, copy]) #Convert tz-aware axis to target time zone.
DataFrame.tz_localize(tz[, axis, level, …])  #Localize tz-naive TimeSeries to target time zone.

作图

DataFrame.plot([x, y, kind, ax, ….])     #DataFrame plotting accessor and method
DataFrame.plot.area([x, y])          #面积图Area plot
DataFrame.plot.bar([x, y])          #垂直条形图Vertical bar plot
DataFrame.plot.barh([x, y])          #水平条形图Horizontal bar plot
DataFrame.plot.box([by])           #箱图Boxplot
DataFrame.plot.density(**kwds)        #核密度Kernel Density Estimate plot
DataFrame.plot.hexbin(x, y[, C, …])      #Hexbin plot
DataFrame.plot.hist([by, bins])        #直方图Histogram
DataFrame.plot.kde(**kwds)          #核密度Kernel Density Estimate plot
DataFrame.plot.line([x, y])          #线图Line plot
DataFrame.plot.pie([y])            #饼图Pie chart
DataFrame.plot.scatter(x, y[, s, c])     #散点图Scatter plot
DataFrame.boxplot([column, by, ax, …])    #Make a box plot from DataFrame column optionally grouped by some columns or
DataFrame.hist(data[, column, by, grid, …])  #Draw histogram of the DataFrame's series using matplotlib / pylab.

转换为其他格式

DataFrame.from_csv(path[, header, sep, …])  #Read CSV file (DEPRECATED, please use pandas.read_csv() instead).
DataFrame.from_dict(data[, orient, dtype])  #Construct DataFrame from dict of array-like or dicts
DataFrame.from_items(items[,columns,orient]) #Convert (key, value) pairs to DataFrame.
DataFrame.from_records(data[, index, …])   #Convert structured or record ndarray to DataFrame
DataFrame.info([verbose, buf, max_cols, …])  #Concise summary of a DataFrame.
DataFrame.to_pickle(path[, compression, …])  #Pickle (serialize) object to input file path.
DataFrame.to_csv([path_or_buf, sep, na_rep]) #Write DataFrame to a comma-separated values (csv) file
DataFrame.to_hdf(path_or_buf, key, **kwargs) #Write the contained data to an HDF5 file using HDFStore.
DataFrame.to_sql(name, con[, flavor, …])   #Write records stored in a DataFrame to a SQL database.
DataFrame.to_dict([orient, into])       #Convert DataFrame to dictionary.
DataFrame.to_excel(excel_writer[, …])     #Write DataFrame to an excel sheet
DataFrame.to_json([path_or_buf, orient, …])  #Convert the object to a JSON string.
DataFrame.to_html([buf, columns, col_space]) #Render a DataFrame as an HTML table.
DataFrame.to_feather(fname)          #write out the binary feather-format for DataFrames
DataFrame.to_latex([buf, columns, …])     #Render an object to a tabular environment table.
DataFrame.to_stata(fname[, convert_dates, …]) #A class for writing Stata binary dta files from array-like objects
DataFrame.to_msgpack([path_or_buf, encoding]) #msgpack (serialize) object to input file path
DataFrame.to_sparse([fill_value, kind])    #Convert to SparseDataFrame
DataFrame.to_dense()             #Return dense representation of NDFrame (as opposed to sparse)
DataFrame.to_string([buf, columns, …])    #Render a DataFrame to a console-friendly tabular output.
DataFrame.to_clipboard([excel, sep])     #Attempt to write text representation of object to the system clipboard This can be pasted into Excel, for example.

到此这篇关于Pandas中DataFrame基本函数整理(小结)的文章就介绍到这了,更多相关Pandas DataFrame基本函数内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 详解pandas.DataFrame.plot() 画图函数

    首先看官网的DataFrame.plot( )函数 DataFrame.plot(x=None, y=None, kind='line', ax=None, subplots=False, sharex=None, sharey=False, layout=None,figsize=None, use_index=True, title=None, grid=None, legend=True, style=None, logx=False, logy=False, loglog=False,

  • python pandas.DataFrame.loc函数使用详解

    官方函数 DataFrame.loc Access a group of rows and columns by label(s) or a boolean array. .loc[] is primarily label based, but may also be used with a boolean array. # 可以使用label值,但是也可以使用布尔值 Allowed inputs are: # 可以接受单个的label,多个label的列表,多个label的切片 A singl

  • pandas dataframe 中的explode函数用法详解

    在使用 pandas 进行数据分析的过程中,我们常常会遇到将一行数据展开成多行的需求,多么希望能有一个类似于 hive sql 中的 explode 函数. 这个函数如下: Code # !/usr/bin/env python # -*- coding:utf-8 -*- # create on 18/4/13 import pandas as pd def dataframe_explode(dataframe, fieldname): temp_fieldname = fieldname

  • python pandas中DataFrame类型数据操作函数的方法

    python数据分析工具pandas中DataFrame和Series作为主要的数据结构. 本文主要是介绍如何对DataFrame数据进行操作并结合一个实例测试操作函数. 1)查看DataFrame数据及属性 df_obj = DataFrame() #创建DataFrame对象 df_obj.dtypes #查看各行的数据格式 df_obj['列名'].astype(int)#转换某列的数据类型 df_obj.head() #查看前几行的数据,默认前5行 df_obj.tail() #查看后几

  • Python中pandas dataframe删除一行或一列:drop函数详解

    用法:DataFrame.drop(labels=None,axis=0, index=None, columns=None, inplace=False) 在这里默认:axis=0,指删除index,因此删除columns时要指定axis=1: inplace=False,默认该删除操作不改变原数据,而是返回一个执行删除操作后的新dataframe: inplace=True,则会直接在原数据上进行删除操作,删除后就回不来了. 例子: >>>df = pd.DataFrame(np.a

  • Pandas中DataFrame基本函数整理(小结)

    构造函数 DataFrame([data, index, columns, dtype, copy]) #构造数据框 属性和数据 DataFrame.axes #index: 行标签:columns: 列标签 DataFrame.as_matrix([columns]) #转换为矩阵 DataFrame.dtypes #返回数据的类型 DataFrame.ftypes #返回每一列的 数据类型float64:dense DataFrame.get_dtype_counts() #返回数据框数据类

  • pandas创建DataFrame的方式小结

    如果你是一个pandas初学者,那么不知道你会不会像我一样.在学用列表或者数组创建DataFrame时理不清怎样用数据生成以及想要形状的的Dataframe,那么,现在,你不用自己琢磨了,我这里给你整理了一下,现在我们就来看看这三种生成Dataframe的方式. 1.用传入列表或者数组创建DataFrame 采用列表创建DataFrame nums = [[i for i in range(3)] for _ in range(10)] nums colu = [f'col_{i}' for i

  • 在Pandas中DataFrame数据合并,连接(concat,merge,join)的实例

    最近在工作中,遇到了数据合并.连接的问题,故整理如下,供需要者参考~ 一.concat:沿着一条轴,将多个对象堆叠到一起 concat方法相当于数据库中的全连接(union all),它不仅可以指定连接的方式(outer join或inner join)还可以指定按照某个轴进行连接.与数据库不同的是,它不会去重,但是可以使用drop_duplicates方法达到去重的效果. concat(objs, axis=0, join='outer', join_axes=None, ignore_ind

  • pandas中DataFrame数据合并连接(merge、join、concat)

    pandas作者Wes McKinney 在[PYTHON FOR DATA ANALYSIS]中对pandas的方方面面都有了一个权威简明的入门级的介绍,但在实际使用过程中,我发现书中的内容还只是冰山一角.谈到pandas数据的行更新.表合并等操作,一般用到的方法有concat.join.merge.但这三种方法对于很多新手来说,都不太好分清使用的场合与用途.今天就pandas官网中关于数据合并和重述的章节做个使用方法的总结. 文中代码块主要有pandas官网教程提供. 1 concat co

  • 浅谈pandas中DataFrame关于显示值省略的解决方法

    python的pandas库是一个非常好的工具,里面的DataFrame更是常用且好用,最近是越用越觉得设计的漂亮,pandas的很多细节设计的都非常好,有待使用过程中发掘. 好了,发完感慨,说一下最近DataFrame遇到的一个细节: 在使用DataFrame中有时候会遇到表格中的value显示不完全,像下面这样: In: import pandas as pd longString = u'''真正的科学家应当是个幻想家:谁不是幻想家,谁就只能把自己称为实践家.人生的磨难是很多的, 所以我们

  • 浅谈pandas中Dataframe的查询方法([], loc, iloc, at, iat, ix)

    pandas为我们提供了多种切片方法,而要是不太了解这些方法,就会经常容易混淆.下面举例对这些切片方法进行说明. 数据介绍 先随机生成一组数据: In [5]: rnd_1 = [random.randrange(1,20) for x in xrange(1000)] ...: rnd_2 = [random.randrange(1,20) for x in xrange(1000)] ...: rnd_3 = [random.randrange(1,20) for x in xrange(1

  • Pandas中DataFrame交换列顺序的方法实现

    一.获取DataFrame列标签 import pandas as pd file_path = '/Users/Arithmetic/da-rnn-master/data/collectd67_power_after_test_smooth.csv' dataset = pd.read_csv(file_path) cols = list(dataset) ['ps_state-stopped', 'ps_state-running', 'ps_state-blocked', 'ps_stat

  • pandas中DataFrame重置索引的几种方法

    在pandas中,经常对数据进行处理 而导致数据索引顺序混乱,从而影响数据读取.插入等. 小笔总结了以下几种重置索引的方法: import pandas as pd import numpy as np df = pd.DataFrame(np.arange(20).reshape((5, 4)),columns=['a', 'b', 'c', 'd']) #得到df: a b c d 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 3 12 13 14 15 4 16 17 1

  • pandas中DataFrame检测重复值的实现

    本文详解如何使用pandas查看dataframe的重复数据,判断是否重复,以及如何去重 DataFrame.duplicated(subset=None, keep='first') subset:如果你认为几个字段重复,则数据重复,就把那几个字段以列表形式放到subset后面.默认是所有字段重复为重复数据. keep: 默认为'first' ,也就是如果有重复数据,则第一条出现的定义为False,后面的重复数据为True. 如果为'last',也就是如果有重复数据,则最后一条出现的定义为Fa

随机推荐