本文部分网络整理,部分个人整理
DataFrame:一个表格型的数据结构,包含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔型等),DataFrame即有行索引也有列索引,可以被看做是由Series组成的字典。
原文链接
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
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.
# 引入DataFrame
import pandas as pd
import numpy as np
date_range() 链接
dates = pd.date_range('20130101', periods=6)
print("dates:\n",dates)
# np.random.seed(0) 固定随机数
df = pd.DataFrame(np.random.randn(6, 4), index = dates,columns=list('ABCD')) # 这里的index是上面的dates
print("df:\n",df)
df2 = pd.DataFrame({'A': 1.,
'B': pd.Timestamp('20130102'),
'C': pd.Series(1, index=list(range(4)), dtype='float32'),
'D': np.array([3] * 4, dtype='int32'),
'E': pd.Categorical(["test", "train", "test", "train"]),
'F': 'foo'})
print("df2:\n",df2)
print("df2.dtypes:\n",df2.dtypes)
print("df.head(2):\n",df.head(2))
print("df.tail(3):\n",df.tail(3))
print("df.index:\n",df.index)
print("df.to_numpy():\n",df.to_numpy())
print("df.describe():\n",df.describe())
print("df.T:\n",df.T)
print("df:\n",df)
print("df.sort_index(axis=1, ascending=False):\n",df.sort_index(axis=1, ascending=False))
# 根据B从小到大排列
print("df.sort_values(by='B'):\n",df.sort_values(by='B'))
print("df['A']:\n",df['A'])
print("df[0:3]:\n",df[0:3])
print("dates[0]:\n",dates[0])
print("df.loc[dates[0]]:\n",df.loc[dates[0]])
print(df.loc[:, ['A', 'B']])
print("df.loc['20130102':'20130104', ['A', 'B']]:\n",df.loc['20130102':'20130104', ['A', 'B']])
print("df.loc['20130102', ['A', 'B']]:\n",df.loc['20130102', ['A', 'B']])
print(df.loc[dates[0], 'A'])
print("df.at[dates[0], 'A']:\n",df.at[dates[0], 'A'])
print("df:\n",df)
print("df.iloc[3]:\n",df.iloc[3])
print("df.iloc[3:5, 0:2]:\n",df.iloc[3:5, 0:2])
print("df.iloc[[1, 2, 4], [0, 2]]:\n",df.iloc[[1, 2, 4], [0, 2]])
print("df.iloc[1:3, :]:\n",df.iloc[1:3, :])
print("df.iloc[:, 1:3]:\n",df.iloc[:, 1:3])
print("df.iloc[1, 1]:\n",df.iloc[1, 1])
print("df.iat[1, 1]:\n",df.iat[1, 1])
print("df[df['A'] > 0]:\n",df[df['A'] > 0])
df3 = df.copy()
print("df3:\n",df3)
df3['E'] = ['one', 'one', 'two', 'three', 'four', 'three']
print("df3:\n",df3)
print("df3[df3['E'].isin(['two', 'four'])]:\n",df3[df3['E'].isin(['two', 'four'])])
s1 = pd.Series([1, 2, 3, 4, 5, 6], index=pd.date_range('20130102', periods=6))
df['F'] = s1
print("df:\n",df)
df.at[dates[0], 'A'] = 0
print(df)
df.iat[0, 1] = 0
print(df)
df.loc[:, 'D'] = np.array([5] * len(df))
print(df)
df4 = df.copy()
df4[df4 > 0] = -df4
print("df4:\n",df4)
df1 = df.reindex(index=dates[0:4], columns=list(df.columns) + ['E'])
print("df1:\n",df1)
df1.loc[dates[0]:dates[1], 'E'] = 1
# 找出没有空的行
print("df1.dropna(how='any'):\n",df1.dropna(how='any'))
# 将所有的空值赋值为5
print("df1.fillna(value=5):\n",df1.fillna(value=5))
print("df.mean():\n",df.mean()) # 求列的平均值
print("df.mean(1):\n",df.mean(1)) # 求行的平均值
print("df:\n",df)
s = pd.Series([1, 3, 5, np.nan, 6, 8], index=dates).shift(2)
# 按 index 索引,df的所有列见s
print(df.sub(s, axis='index'))
# df.apply(np.cumsum) 按行求和,第二行为df前两行之和,第三行为df前三行之和
print("df.apply(np.cumsum):\n",df.apply(np.cumsum))
# df.apply(lambda x: x.max() - x.min()) 返回每一列的最大值与最小值之差
print("df.apply(lambda x: x.max() - x.min()):\n",df.apply(lambda x: x.max() - x.min()))
left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})
right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})
print("left:\n",left)
print("right:\n",right)
print("pd.merge(left, right, on='key'):\n",pd.merge(left, right, on='key'))
left1 = pd.DataFrame({'key': ['foo', 'bar'], 'lval': [1, 2]})
right1 = pd.DataFrame({'key': ['foo', 'bar'], 'rval': [4, 5]})
print("left1:\n",left1)
print("right1:\n",right1)
print("pd.merge(left1, right1, on='key'):\n",pd.merge(left1, right1, on='key'))
df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'three',
'two', 'two', 'one', 'three'],
'C': np.random.randn(8),
'D': np.random.randn(8)})
print("df:\n",df)
print("df.groupby('A').sum():\n",df.groupby('A').sum())
print(df.groupby(['A', 'B']).sum())
MultiIndex.from_tuples函数详解链接
tuples = list(zip(*[['bar', 'bar', 'baz', 'baz',
'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two',
'one', 'two', 'one', 'two']]))
# print("tuples:\n",tuples)
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
# print("index:\n",index)
np.random.seed(0)
df4 = pd.DataFrame(np.random.randn(8, 2), index=index, columns=['A', 'B'])
print("df4:\n",df4)
df5 = df[:4]
print("df5:\n",df5)
stacked = df5.stack()
print("stacked:\n",stacked)
print("stacked.unstack():\n",stacked.unstack())
df = pd.DataFrame({'A': ['one', 'one', 'two', 'three'] * 3,
'B': ['A', 'B', 'C'] * 4,
'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 2,
'D': np.random.randn(12),
'E': np.random.randn(12)})
print(pd.pivot_table(df, values='D', index=['A', 'B'], columns=['C']))
df6 = pd.DataFrame({"id": [1, 2, 3, 4, 5, 6],
"raw_grade": ['a', 'b', 'b', 'a', 'a', 'e']})
print("df6:\n",df6)
df6["grade"] = df6["raw_grade"].astype("category")
print("df6[grade]:\n",df6["grade"])
df6["grade"].cat.categories = ["very good", "good", "very bad"]
print("df6[grade].cat.categories:\n",df6["grade"].cat.categories)
print("df6.sort_values(by=grade):\n",df6.sort_values(by="grade"))
print("df.groupby(grade).size():\n",df6.groupby("grade").size())