打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【python】Pandas中DataFrame基本函数整理(全)
构造函数
DataFrame([data, index, columns, dtype, copy]) #构造数据框
属性和数据
DataFrame.axes                                #index: 行标签;columns: 列标签DataFrame.as_matrix([columns])                #转换为矩阵DataFrame.dtypes                              #返回数据的类型DataFrame.ftypes                              #返回每一列的 数据类型float64:denseDataFrame.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 axisDataFrame.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.ltDataFrame.gt(other[, axis, level])            #类似Array.gtDataFrame.le(other[, axis, level])            #类似Array.leDataFrame.ge(other[, axis, level])            #类似Array.geDataFrame.ne(other[, axis, level])            #类似Array.neDataFrame.eq(other[, axis, level])            #类似Array.eqDataFrame.combine(other,func[,fill_value, …]) #Add two DataFrame objects and do not propagate NaN values, so if for aDataFrame.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/callablesDataFrame.transform(func, *args, **kwargs)    #Call function producing a like-indexed NDFrameDataFrame.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 axisDataFrame.any([axis, bool_only, skipna])      #Return whether any element is True over requested axisDataFrame.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 objectDataFrame.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 theDataFrame.drop(labels[, axis, level, …])      #返回删除的列DataFrame.drop_duplicates([subset, keep, …])  #Return DataFrame with duplicate rows removed, optionally onlyDataFrame.duplicated([subset, keep])          #Return boolean Series denoting duplicate rows, optionally onlyDataFrame.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 criteriaDataFrame.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.takeDataFrame.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 anyDataFrame.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 axisDataFrame.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 axisDataFrame.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, optionallyDataFrame.T                                   #Transpose index and columnsDataFrame.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 withoutDataFrame.shift([periods, freq, axis])        #Shift index by desired number of periods with an optional time freqDataFrame.first_valid_index()                 #Return label for first non-NA/null valueDataFrame.last_valid_index()                  #Return label for last non-NA/null valueDataFrame.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 desiredDataFrame.to_timestamp([freq, how, axis])     #Cast to DatetimeIndex of timestamps, at beginning of periodDataFrame.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 methodDataFrame.plot.area([x, y])                   #面积图Area plotDataFrame.plot.bar([x, y])                    #垂直条形图Vertical bar plotDataFrame.plot.barh([x, y])                   #水平条形图Horizontal bar plotDataFrame.plot.box([by])                      #箱图BoxplotDataFrame.plot.density(**kwds)                #核密度Kernel Density Estimate plotDataFrame.plot.hexbin(x, y[, C, …])           #Hexbin plotDataFrame.plot.hist([by, bins])               #直方图HistogramDataFrame.plot.kde(**kwds)                    #核密度Kernel Density Estimate plotDataFrame.plot.line([x, y])                   #线图Line plotDataFrame.plot.pie([y])                       #饼图Pie chartDataFrame.plot.scatter(x, y[, s, c])          #散点图Scatter plotDataFrame.boxplot([column, by, ax, …])        #Make a box plot from DataFrame column optionally grouped by some columns orDataFrame.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 dictsDataFrame.from_items(items[,columns,orient])  #Convert (key, value) pairs to DataFrame.DataFrame.from_records(data[, index, …])      #Convert structured or record ndarray to DataFrameDataFrame.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) fileDataFrame.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 sheetDataFrame.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 DataFramesDataFrame.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 objectsDataFrame.to_msgpack([path_or_buf, encoding]) #msgpack (serialize) object to input file pathDataFrame.to_sparse([fill_value, kind])       #Convert to SparseDataFrameDataFrame.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.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
pandas中行列转换
Pandas数据分析
3.DataFrame的增删改查
用Python自动实现图表可视化操作,提高工作效率,又能有更多的时间摸鱼了~
Python 数据分析包:pandas 基础
经典量化交易策略之羊驼和均线策略
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服