DataFrame对象是一个由行列组成的表。DataFrame中行由columns组成,列由index组成,它们都是Index对象。它的值还是numpy数组。

  1. import pandas as pd
  2. data = {'name':['ming', 'hong', 'gang', 'tian'], 'age':[12, 13, 14, 20], 'score':[80.3, 88.2, 90, 99.9]}
  3. df1 = pd.DataFrame(data,index=range(1,5))
  4. print(df1)

dateframe 包括行和列的标签
name age score
1 ming 12 80.3
2 hong 13 88.2
3 gang 14 90.0
4 tian 20 99.9

  1. print(df1.index) # RangeIndex(start=1, stop=5, step=1)
  2. print(df1.columns) # Index(['name', 'age', 'score'], dtype='object')
  3. print(df1.values)

df1.values 不包括行和列标签
[[‘ming’ 12 80.3]
[‘hong’ 13 88.2]
[‘gang’ 14 90.0]
[‘tian’ 20 99.9]]

DataFrame对象进行索引

1:使用columns的值对列进行索引

直接使用columns中的值进行索引,得到的是一列或者是多列的值

  1. print(df1['name'])

1 ming
2 hong
3 gang
4 tian
Name: name, dtype: object

  1. print(df1[['name','age']])

name age
1 ming 12
2 hong 13
3 gang 14
4 tian 20
注意:不可以直接使用下标对列进行索引,除非该columns当中包含该值。如下面的操作是错误的

  1. print(df1[0]) # 结果: 错误

2:切片或者布尔Series对行进行索引

使用切片索引,或者布尔类型Series进行索引:

  1. print(df1[0:3])

name age score
1 ming 12 80.3
2 hong 13 88.2
3 gang 14 90.0

  1. print(df1[ df1['age'] > 13 ])

name age score
3 gang 14 90.0
4 tian 20 99.9

3:使用loc和iloc进行索引

本质上loc是用index和columns当中的值进行索引,而iloc是不理会index和columns当中的值的,永远都是用从0开始的下标进行索引。所以当你搞懂这句话的时候,下面的索引就会变得非常简单:

  1. print(df1.loc[3]) # 返回行索引为 3 的数据

name gang
age 14
score 90
Name: 3, dtype: object

  1. print(df1.loc[:,'age']) # 返回列索引为age的数据

1 12
2 13
3 14
4 20
Name: age, dtype: int64

  1. print(df1.iloc[3]) # iloc不理会index和columns中的值,从0开始的下标进行索引

name tian
age 20
score 99.9
Name: 4, dtype: object

  1. print(df1.iloc[:,1]) # 返回列索引为 1 的数据

1 12
2 13
3 14
4 20
Name: age, dtype: int64

DataFrame


Constructor

DataFrame([data, index, columns, dtype, copy]) Two-dimensional, size-mutable, potentially heterogeneous tabular data.

Attributes and underlying data属性与基础性数据

Axes

DataFrame.index DataFrame的行索引
RangeIndex(start=0, stop=3864, step=1)
DataFrame.columns DataFrame 的列标签
Index([‘日期’, ‘股票代码’, ‘名称’, ‘收盘价’, ‘最高价’, ‘最低价’, ‘开盘价’, ‘前收盘’, ‘涨跌额’, ‘涨跌幅’,
‘换手率’, ‘成交量’, ‘成交金额’, ‘总市值’, ‘流通市值’],
dtype=’object’)
DataFrame.dtypes DataFrame每个列的数据类型
DataFrame.info([verbose, buf, max_cols, …]) Print a concise summary of a DataFrame.
DataFrame.select_dtypes([include, exclude]) Return a subset of the DataFrame’s columns based on the column dtypes.
DataFrame.values 返回DataFrame值的数组表示
DataFrame.axes 返回 DataFrame轴的列表表示
[RangeIndex(start=0, stop=3864, step=1), Index([‘日期’, ‘股票代码’, ‘名称’, ‘收盘价’, ‘最高价’, ‘最低价’, ‘开盘价’, ‘前收盘’, ‘涨跌额’, ‘涨跌幅’,
‘换手率’, ‘成交量’, ‘成交金额’, ‘总市值’, ‘流通市值’],
dtype=’object’)]
DataFrame.ndim Return an int representing the number of axes / array dimensions.
DataFrame.size 返回元素的数量57960
DataFrame.shape 返回元组表示的DataFrame的形状,行,列数量
(3864, 15)
DataFrame.memory_usage([index, deep]) 返回每列数据的内存占用
DataFrame.empty 指明Dataframe是否为空,布尔值

Conversion 转换

DataFrame.astype(dtype[, copy, errors]) 将pandas对象强制转换为指定的数据类型 dtype.
DataFrame.convert_dtypes([infer_objects, …]) 使用支持的数据类型将列转换为最佳的数据类型 pd.NA.
DataFrame.infer_objects() 尝试为对象列推断更好的数据类型。
DataFrame.copy([deep]) 复制此对象的索引和数据。
DataFrame.bool() 返回单个元素系列或数据帧的布尔值。

Indexing, iteration 索引,迭代

DataFrame.head([n]) 返回开头的n条数据,默认5条
DataFrame.at Access a single value for a row/column label pair.
DataFrame.iat Access a single value for a row/column pair by integer position.
DataFrame.loc Access a group of rows and columns by label(s) or a boolean array.
DataFrame.iloc Purely integer-location based indexing for selection by position.
DataFrame.insert(loc, column, value[, …]) Insert column into DataFrame at specified location.
DataFrame.__iter__() Iterate over info axis.
DataFrame.items() 返回列名与 Series对的生成器对象
DataFrame.iteritems() Iterate over (column name, Series) pairs.
DataFrame.keys() 返回列名的列表
[‘日期’, ‘股票代码’, ‘名称’, ‘收盘价’, ‘最高价’, ‘最低价’, ‘开盘价’, ‘前收盘’, ‘涨跌额’, ‘涨跌幅’, ‘换手率’, ‘成交量’, ‘成交金额’, ‘总市值’, ‘流通市值’]
DataFrame.iterrows() Iterate over DataFrame rows as (index, Series) pairs.
DataFrame.itertuples([index, name]) Iterate over DataFrame rows as namedtuples.
DataFrame.lookup(row_labels, col_labels) Label-based “fancy indexing” function for DataFrame.
DataFrame.pop(item) Return item and drop from frame.
DataFrame.tail([n]) 返回最后的n条记录,默认5条
DataFrame.xs(key[, axis, level, drop_level]) Return cross-section from the Series/DataFrame.
DataFrame.get(key[, default]) 根据给定的key返回该列数据
DataFrame.isin(values) 判定DataFrame中的每个元素是否包含给定的值,返回布尔值
DataFrame.where(cond[, other, inplace, …]) 当条件为False时替换值.
DataFrame.mask(cond[, other, inplace, axis, …]) Replace values where the condition is True.
DataFrame.query(expr[, inplace]) 根据一个布尔型的表达式查询DataFrame 的列

For more information on .at, .iat, .loc, and .iloc, see the indexing documentation.

Binary operator functions 二元运算符函数

DataFrame.add(other[, axis, level, fill_value]) Get Addition of dataframe and other, element-wise (binary operator add).
DataFrame.sub(other[, axis, level, fill_value]) Get Subtraction of dataframe and other, element-wise (binary operator sub).
DataFrame.mul(other[, axis, level, fill_value]) Get Multiplication of dataframe and other, element-wise (binary operator mul).
DataFrame.div(other[, axis, level, fill_value]) Get Floating division of dataframe and other, element-wise (binary operator truediv).
DataFrame.truediv(other[, axis, level, …]) Get Floating division of dataframe and other, element-wise (binary operator truediv).
DataFrame.floordiv(other[, axis, level, …]) Get Integer division of dataframe and other, element-wise (binary operator floordiv).
DataFrame.mod(other[, axis, level, fill_value]) Get Modulo of dataframe and other, element-wise (binary operator mod).
DataFrame.pow(other[, axis, level, fill_value]) Get Exponential power of dataframe and other, element-wise (binary operator pow).
DataFrame.dot(other) Compute the matrix multiplication between the DataFrame and other.
DataFrame.radd(other[, axis, level, fill_value]) Get Addition of dataframe and other, element-wise (binary operator radd).
DataFrame.rsub(other[, axis, level, fill_value]) Get Subtraction of dataframe and other, element-wise (binary operator rsub).
DataFrame.rmul(other[, axis, level, fill_value]) Get Multiplication of dataframe and other, element-wise (binary operator rmul).
DataFrame.rdiv(other[, axis, level, fill_value]) Get Floating division of dataframe and other, element-wise (binary operator rtruediv).
DataFrame.rtruediv(other[, axis, level, …]) Get Floating division of dataframe and other, element-wise (binary operator rtruediv).
DataFrame.rfloordiv(other[, axis, level, …]) Get Integer division of dataframe and other, element-wise (binary operator rfloordiv).
DataFrame.rmod(other[, axis, level, fill_value]) Get Modulo of dataframe and other, element-wise (binary operator rmod).
DataFrame.rpow(other[, axis, level, fill_value]) Get Exponential power of dataframe and other, element-wise (binary operator rpow).
DataFrame.lt(other[, axis, level]) Get Less than of dataframe and other, element-wise (binary operator lt).
DataFrame.gt(other[, axis, level]) Get Greater than of dataframe and other, element-wise (binary operator gt).
DataFrame.le(other[, axis, level]) Get Less than or equal to of dataframe and other, element-wise (binary operator le).
DataFrame.ge(other[, axis, level]) Get Greater than or equal to of dataframe and other, element-wise (binary operator ge).
DataFrame.ne(other[, axis, level]) Get Not equal to of dataframe and other, element-wise (binary operator ne).
DataFrame.eq(other[, axis, level]) Get Equal to of dataframe and other, element-wise (binary operator eq).
DataFrame.combine(other, func[, fill_value, …]) Perform column-wise combine with another DataFrame.
DataFrame.combine_first(other) Update null elements with value in the same location in other.

Function application, GroupBy & window

函数应用程序 分组和窗体

DataFrame.apply(func[, axis, raw, …]) Apply a function along an axis of the DataFrame.
DataFrame.applymap(func) Apply a function to a Dataframe elementwise.
DataFrame.pipe(func, args, *kwargs) Apply func(self, args, *kwargs).
DataFrame.agg([func, axis]) 在指定轴上使用一个或多个操作进行聚合。
DataFrame.aggregate([func, axis]) Aggregate using one or more operations over the specified axis.
DataFrame.transform(func[, axis]) Call func on self producing a DataFrame with transformed values.
DataFrame.groupby([by, axis, level, …]) 使用映射器或按一系列列对数据帧进行分组。
DataFrame.rolling(window[, min_periods, …]) Provide rolling window calculations.
DataFrame.expanding([min_periods, center, axis]) Provide expanding transformations.
DataFrame.ewm([com, span, halflife, alpha, …]) Provide exponential weighted (EW) functions.

Computations / descriptive stats 计算/描述性统计

DataFrame.abs() 返回一个带有每个元素绝对数值的序列/数据帧。
DataFrame.all([axis, bool_only, skipna, level]) 返回是否所有元素都为真,可能在轴上。
DataFrame.any([axis, bool_only, skipna, level]) 返回任何元素是否为真
DataFrame.clip([lower, upper, axis, inplace]) Trim values at input threshold(s).
DataFrame.corr([method, min_periods]) Compute pairwise correlation of columns, excluding NA/null values.
DataFrame.corrwith(other[, axis, drop, method]) Compute pairwise correlation.
DataFrame.count([axis, level, numeric_only]) 计算每列或每行的非NA单元格数。
DataFrame.cov([min_periods, ddof]) Compute pairwise covariance of columns, excluding NA/null values.
DataFrame.cummax([axis, skipna]) 返回数据帧或系列轴上的累积最大值。
DataFrame.cummin([axis, skipna]) 返回数据帧或系列轴上的累积最小值。
DataFrame.cumprod([axis, skipna]) 返回数据帧或系列轴上的累乘积。
DataFrame.cumsum([axis, skipna]) Return cumulative sum over a DataFrame or Series axis.
DataFrame.describe([percentiles, include, …]) 生成描述性统计。count、mean、std、min、max等
DataFrame.diff([periods, axis]) First discrete difference of element.
DataFrame.eval(expr[, inplace]) Evaluate a string describing operations on DataFrame columns.
DataFrame.kurt([axis, skipna, level, …]) Return unbiased kurtosis over requested axis.
DataFrame.kurtosis([axis, skipna, level, …]) Return unbiased kurtosis over requested axis.
DataFrame.mad([axis, skipna, level]) Return the mean absolute deviation of the values for the requested axis.
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, dropna]) Get the mode(s) of each element along the selected axis.
DataFrame.pct_change([periods, fill_method, …]) Percentage change between the current and a prior element.
DataFrame.prod([axis, skipna, level, …]) Return the product of the values for the requested axis.
DataFrame.product([axis, skipna, level, …]) Return the product of the values for the requested axis.
DataFrame.quantile([q, axis, numeric_only, …]) Return values at the given quantile over requested axis.
DataFrame.rank([axis, method, numeric_only, …]) Compute numerical data ranks (1 through n) along axis.
DataFrame.round([decimals]) Round a DataFrame to a variable number of decimal places.
DataFrame.sem([axis, skipna, level, ddof, …]) Return unbiased standard error of the mean over requested axis.
DataFrame.skew([axis, skipna, level, …]) Return unbiased skew over requested axis.
DataFrame.sum([axis, skipna, level, …]) 返回所请求轴的值的总和。
DataFrame.std([axis, skipna, level, ddof, …]) 请求返回的轴超出标准偏差。
DataFrame.var([axis, skipna, level, ddof, …]) Return unbiased variance over requested axis.
DataFrame.nunique([axis, dropna]) Count distinct observations over requested axis.
DataFrame.value_counts([subset, normalize, …]) Return a Series containing counts of unique rows in the DataFrame.

Reindexing / selection / label manipulation

重新编制索引/选择/标签操作

DataFrame.add_prefix(prefix) Prefix labels with string prefix.
DataFrame.add_suffix(suffix) Suffix labels with string suffix.
DataFrame.align(other[, join, axis, level, …]) Align two objects on their axes with the specified join method.
DataFrame.at_time(time[, asof, axis]) Select values at particular time of day (e.g., 9:30AM).
DataFrame.between_time(start_time, end_time) Select values between particular times of the day (e.g., 9:00-9:30 AM).
DataFrame.drop([labels, axis, index, …]) Drop specified labels from rows or columns.
DataFrame.drop_duplicates([subset, keep, …]) Return DataFrame with duplicate rows removed.
DataFrame.duplicated([subset, keep]) Return boolean Series denoting duplicate rows.
DataFrame.equals(other) Test whether two objects contain the same elements.
DataFrame.filter([items, like, regex, axis]) Subset the dataframe rows or columns according to the specified index labels.
DataFrame.first(offset) Select initial periods of time series data based on a date offset.
DataFrame.head([n]) Return the first n rows.
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) Select final periods of time series data based on a date offset.
DataFrame.reindex(**kwargs) Conform Series/DataFrame to new index with optional filling logic.
DataFrame.reindex_like(other[, method, …]) Return an object with matching indices as other object.
DataFrame.rename(**kwargs) Alter axes labels.
DataFrame.rename_axis(**kwargs) Set the name of the axis for the index or columns.
DataFrame.reset_index([level, drop, …]) Reset the index, or a level of it.
DataFrame.sample([n, frac, replace, …]) Return a random sample of items from an axis of object.
DataFrame.set_axis(labels[, axis, inplace]) Assign desired index to given axis.
DataFrame.set_index(keys[, drop, append, …]) Set the DataFrame index using existing columns.
DataFrame.tail([n]) Return the last n rows.
DataFrame.take(indices[, axis, is_copy]) Return the elements in the given positional indices along an axis.
DataFrame.truncate([before, after, axis, copy]) Truncate a Series or DataFrame before and after some index value.

Missing data handling 缺失数据处理

DataFrame.backfill([axis, inplace, limit, …]) Synonym for DataFrame.fillna() with method='bfill'.
DataFrame.bfill([axis, inplace, limit, downcast]) Synonym for DataFrame.fillna() with method='bfill'.
DataFrame.dropna([axis, how, thresh, …]) Remove missing values.
DataFrame.ffill([axis, inplace, limit, downcast]) Synonym for DataFrame.fillna() with method='ffill'.
DataFrame.fillna([value, method, axis, …]) Fill NA/NaN values using the specified method.
DataFrame.interpolate([method, axis, limit, …]) Please note that only method='linear' is supported for DataFrame/Series with a MultiIndex.
DataFrame.isna() Detect missing values.
DataFrame.isnull() Detect missing values.
DataFrame.notna() Detect existing (non-missing) values.
DataFrame.notnull() Detect existing (non-missing) values.
DataFrame.pad([axis, inplace, limit, downcast]) Synonym for DataFrame.fillna() with method='ffill'.
DataFrame.replace([to_replace, value, …]) Replace values given in to_replace with value.

Reshaping, sorting, transposing

DataFrame.droplevel(level[, axis]) Return DataFrame with requested index / column level(s) removed.
DataFrame.pivot([index, columns, values]) Return reshaped DataFrame organized by given index / column values.
DataFrame.pivot_table([values, index, …]) Create a spreadsheet-style pivot table as a DataFrame.
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]) Return the first n rows ordered by columns in descending order.
DataFrame.nsmallest(n, columns[, keep]) Return the first n rows ordered by columns in ascending order.
DataFrame.swaplevel([i, j, axis]) Swap levels i and j in a MultiIndex on a particular axis.
DataFrame.stack([level, dropna]) Stack the prescribed level(s) from columns to index.
DataFrame.unstack([level, fill_value]) Pivot a level of the (necessarily hierarchical) index labels.
DataFrame.swapaxes(axis1, axis2[, copy]) Interchange axes and swap values axes appropriately.
DataFrame.melt([id_vars, value_vars, …]) Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.
DataFrame.explode(column[, ignore_index]) Transform each element of a list-like to a row, replicating index values.
DataFrame.squeeze([axis]) Squeeze 1 dimensional axis objects into scalars.
DataFrame.to_xarray() Return an xarray object from the pandas object.
DataFrame.T
DataFrame.transpose(*args[, copy]) Transpose index and columns.

Combining / comparing / joining / merging

合并/比较/加入/合并

DataFrame.append(other[, ignore_index, …]) Append rows of other to the end of caller, returning a new object.
DataFrame.assign(**kwargs) Assign new columns to a DataFrame.
DataFrame.compare(other[, align_axis, …]) Compare to another DataFrame and show the differences.
DataFrame.join(other[, on, how, lsuffix, …]) Join columns of another DataFrame.
DataFrame.merge(right[, how, on, left_on, …]) Merge DataFrame or named Series objects with a database-style join.
DataFrame.update(other[, join, overwrite, …]) Modify in place using non-NA values from another DataFrame.

Time Series-related 时间序列相关

DataFrame.asfreq(freq[, method, how, …]) Convert TimeSeries to specified frequency.
DataFrame.asof(where[, subset]) Return the last row(s) without any NaNs before where.
DataFrame.shift([periods, freq, axis, …]) Shift index by desired number of periods with an optional time freq.
DataFrame.slice_shift([periods, axis]) Equivalent to shift without copying data.
DataFrame.tshift([periods, freq, axis]) (DEPRECATED) Shift the time index, using the index’s frequency if available.
DataFrame.first_valid_index() Return index for first non-NA/null value.
DataFrame.last_valid_index() Return index for last non-NA/null value.
DataFrame.resample(rule[, axis, closed, …]) Resample time-series data.
DataFrame.to_period([freq, axis, copy]) Convert DataFrame from DatetimeIndex to PeriodIndex.
DataFrame.to_timestamp([freq, how, axis, copy]) 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 index of a Series or DataFrame to target time zone.

Metadata

DataFrame.attrs is a dictionary for storing global metadata for this DataFrame.
Warning
DataFrame.attrs is considered experimental and may change without warning.

DataFrame.attrs Dictionary of global attributes on this object.

Plotting

DataFrame.plot is both a callable method and a namespace attribute for specific plotting methods of the form DataFrame.plot.<kind>.

DataFrame.plot([x, y, kind, ax, ….]) DataFrame plotting accessor and method
DataFrame.plot.area([x, y]) Draw a stacked area plot.
DataFrame.plot.bar([x, y]) Vertical bar plot.
DataFrame.plot.barh([x, y]) Make a horizontal bar plot.
DataFrame.plot.box([by]) Make a box plot of the DataFrame columns.
DataFrame.plot.density([bw_method, ind]) Generate Kernel Density Estimate plot using Gaussian kernels.
DataFrame.plot.hexbin(x, y[, C, …]) Generate a hexagonal binning plot.
DataFrame.plot.hist([by, bins]) Draw one histogram of the DataFrame’s columns.
DataFrame.plot.kde([bw_method, ind]) Generate Kernel Density Estimate plot using Gaussian kernels.
DataFrame.plot.line([x, y]) Plot Series or DataFrame as lines.
DataFrame.plot.pie(**kwargs) Generate a pie plot.
DataFrame.plot.scatter(x, y[, s, c]) Create a scatter plot with varying marker point size and color.
DataFrame.boxplot([column, by, ax, …]) Make a box plot from DataFrame columns.
DataFrame.hist([column, by, grid, …]) Make a histogram of the DataFrame’s.

Sparse accessor

Sparse-dtype specific methods and attributes are provided under the DataFrame.sparse accessor.

DataFrame.sparse.density Ratio of non-sparse points to total (dense) data points.
DataFrame.sparse.from_spmatrix(data[, …]) Create a new DataFrame from a scipy sparse matrix.
DataFrame.sparse.to_coo() Return the contents of the frame as a sparse SciPy COO matrix.
DataFrame.sparse.to_dense() Convert a DataFrame with sparse values to dense.

Serialization / IO / conversion 序列化/IO/转换

DataFrame.from_dict(data[, orient, dtype, …]) Construct DataFrame from dict of array-like or dicts.
DataFrame.from_records(data[, index, …]) Convert structured or record ndarray to DataFrame.
DataFrame.to_parquet(**kwargs) Write a DataFrame to the binary parquet format.
DataFrame.to_pickle(path[, compression, …]) Pickle (serialize) object to file.
DataFrame.to_csv([path_or_buf, sep, na_rep, …]) Write object to a comma-separated values (csv) file.
DataFrame.to_hdf(path_or_buf, key[, mode, …]) Write the contained data to an HDF5 file using HDFStore.
DataFrame.to_sql(name, con[, schema, …]) Write records stored in a DataFrame to a SQL database.
DataFrame.to_dict([orient, into]) Convert the DataFrame to a dictionary.
DataFrame.to_excel(excel_writer[, …]) Write object 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(**kwargs) Write a DataFrame to the binary Feather format.
DataFrame.to_latex([buf, columns, …]) Render object to a LaTeX tabular, longtable, or nested table/tabular.
DataFrame.to_stata(**kwargs) Export DataFrame object to Stata dta format.
DataFrame.to_gbq(destination_table[, …]) Write a DataFrame to a Google BigQuery table.
DataFrame.to_records([index, column_dtypes, …]) Convert DataFrame to a NumPy record array.
DataFrame.to_string([buf, columns, …]) Render a DataFrame to a console-friendly tabular output.
DataFrame.to_clipboard([excel, sep]) Copy object to the system clipboard.
DataFrame.to_markdown([buf, mode, index]) Print DataFrame in Markdown-friendly format.
DataFrame.style Returns a Styler object.