【Python】Pandas中DataFrame基本函数及举例整理

本文部分网络整理,部分个人整理

介绍

DataFrame:一个表格型的数据结构,包含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔型等),DataFrame即有行索引也有列索引,可以被看做是由Series组成的字典。

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.

DataFrame基本函数举例

导入包

# 引入DataFrame 
import pandas as pd
import numpy as np

pandas.DataFrame

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)

在这里插入图片描述

pandas.DataFrame.dtypes

print("df2.dtypes:\n",df2.dtypes)

在这里插入图片描述

pandas.DataFrame.head

print("df.head(2):\n",df.head(2))

在这里插入图片描述

pandas.DataFrame.tail

print("df.tail(3):\n",df.tail(3))

在这里插入图片描述

pandas.DataFrame.index

print("df.index:\n",df.index)

在这里插入图片描述

pandas.DataFrame.to_numpy

print("df.to_numpy():\n",df.to_numpy())

在这里插入图片描述

pandas.DataFrame.describe

print("df.describe():\n",df.describe())

在这里插入图片描述

pandas.DataFrame.T

print("df.T:\n",df.T)

在这里插入图片描述

pandas.DataFrame.sort_index

print("df:\n",df)
print("df.sort_index(axis=1, ascending=False):\n",df.sort_index(axis=1, ascending=False))

在这里插入图片描述

pandas.DataFrame.sort_values

# 根据B从小到大排列
print("df.sort_values(by='B'):\n",df.sort_values(by='B'))

在这里插入图片描述

getting

df[‘A’]

print("df['A']:\n",df['A'])

在这里插入图片描述

df[0:3]

print("df[0:3]:\n",df[0:3])

在这里插入图片描述

pandas.DataFrame.loc

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'])

在这里插入图片描述

pandas.DataFrame.at

print("df.at[dates[0], 'A']:\n",df.at[dates[0], 'A'])

在这里插入图片描述

pandas.DataFrame.iloc

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])

在这里插入图片描述

pandas.DataFrame.iat

print("df.iat[1, 1]:\n",df.iat[1, 1])

在这里插入图片描述

df[df[‘A’] > 0]

print("df[df['A'] > 0]:\n",df[df['A'] > 0])

在这里插入图片描述

df[df > 0]

在这里插入图片描述

pandas.DataFrame.copy

df3 = df.copy()
print("df3:\n",df3)

在这里插入图片描述

df3[‘E’]

df3['E'] = ['one', 'one', 'two', 'three', 'four', 'three']
print("df3:\n",df3)

在这里插入图片描述

pandas.DataFrame.isin

print("df3[df3['E'].isin(['two', 'four'])]:\n",df3[df3['E'].isin(['two', 'four'])])

在这里插入图片描述

DataFrame中的赋值

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)

在这里插入图片描述

pandas.DataFrame.reindex

df1 = df.reindex(index=dates[0:4], columns=list(df.columns) + ['E'])
print("df1:\n",df1)

在这里插入图片描述

df1.loc[dates[0]:dates[1], 'E'] = 1

在这里插入图片描述

pandas.DataFrame.dropna

# 找出没有空的行
print("df1.dropna(how='any'):\n",df1.dropna(how='any'))

在这里插入图片描述

pandas.DataFrame.fillna 给DataFrame中的空值赋值

# 将所有的空值赋值为5
print("df1.fillna(value=5):\n",df1.fillna(value=5))

在这里插入图片描述

pandas.DataFrame.mean

print("df.mean():\n",df.mean()) # 求列的平均值

在这里插入图片描述

print("df.mean(1):\n",df.mean(1)) # 求行的平均值

在这里插入图片描述

pandas.DataFrame.sub 减法,元素指向

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'))

pd.Series
在这里插入图片描述

在这里插入图片描述

pandas.DataFrame.apply 函数应用

# 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()))

在这里插入图片描述

pandas.DataFrame.merge 两个合并对象的DataFrame

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'))

在这里插入图片描述

pandas.DataFrame.groupby.sum 分类求和

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())

在这里插入图片描述

pandas.DataFrame.stack

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)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

pandas.DataFrame.unstack

print("stacked.unstack():\n",stacked.unstack())

在这里插入图片描述

pandas.DataFrame.pivot_table

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']))

在这里插入图片描述

pandas.DataFrame.astype

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"])

在这里插入图片描述

pandas.DataFrame.cat.categories

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"))

在这里插入图片描述

pandas.DataFrame.groupby

print("df.groupby(grade).size():\n",df6.groupby("grade").size())

在这里插入图片描述

  • 3
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在 Python ,如果你有一个名为 `df` 的 dataframe 对象,你可以使用 `df.tail()` 函数来输出 dataframe 的最后几行。例如: ``` import pandas as pd # 建立一个简单的 dataframe df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [6, 7, 8, 9, 10]}) # 输出 dataframe 的最后两行 print(df.tail(2)) ``` 输出结果如下: ``` A B 3 4 9 4 5 10 ``` 注意,默认情况下 `tail()` 函数会输出 dataframe 的最后五行,你也可以通过传入一个数字来指定输出 dataframe 的最后几行。 ### 回答2: 在Python,可以使用tail函数来输出DataFrame对象的最后几行。下面是一个使用tail函数的例子: 假设我们有一个包含学生考试成绩的DataFrame对象,其包括学生的姓名、班级和分数。我们可以使用tail函数来输出最后几个学生的成绩记录。 首先,我们需要导入pandas库来使用DataFrame对象和tail函数: ``` import pandas as pd ``` 然后,我们创建一个包含学生成绩的DataFrame对象: ``` data = {'姓名': ['张三', '李四', '王五', '赵六', '钱七'], '班级': ['一班', '一班', '二班', '三班', '三班'], '分数': [90, 85, 95, 88, 92]} df = pd.DataFrame(data) ``` 现在我们可以使用tail函数来输出最后2个学生的成绩记录: ``` print(df.tail(2)) ``` 运行以上代码,输出如下: ``` 姓名 班级 分数 3 赵六 三班 88 4 钱七 三班 92 ``` 从结果可以看出,tail函数按照默认的5行输出了最后2个学生的成绩记录。如果你想要输出更多或者更少的行,可以在tail函数传入想要输出的行数作为参数。 ### 回答3: 在PythonDataFramepandas用来存储和操作数据的一种数据结构。tail函数DataFrame对象的一个方法,用于返回DataFrame对象的最后几行。 下面是一个使用tail函数输出的示例: ```python import pandas as pd # 创建一个DataFrame对象 data = {'Name': ['John', 'Emma', 'Grace', 'David', 'Jessica'], 'Age': [23, 28, 32, 19, 25], 'City': ['New York', 'London', 'Paris', 'Tokyo', 'Sydney']} df = pd.DataFrame(data) # 输出DataFrame对象的后两行 tail_df = df.tail(2) print(tail_df) ``` 运行上述代码,输出结果为: ``` Name Age City 3 David 19 Tokyo 4 Jessica 25 Sydney ``` 在上述示例,我们首先创建了一个包含姓名、年龄和城市信息的DataFrame对象。然后使用tail函数,指定返回最后两行的数据,并将结果赋给tail_df。最后,我们打印出tail_df,即输出了DataFrame对象的最后两行数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Performer_Cherry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值