通过索引排序,默认是纵轴索引值,升序
df.sort_index(axis=0,ascending=True)
通过数值排序
df.sort_values(by,axis=0,ascending=True)
by可以是单个列标签,也可以是多个列标签的列表
合并DataFrame merge 原理像sql 的两表关联 join pd1=pd.DataFrame(list1,columns=['userid',]) pd2=pd.DataFrame(list2,columns=['r','userid2','filialename','username','useraddress',]) pd3=pd.merge(pd1,pd2,how='left',left_on='userid',right_on='userid2') how,连接方式'left','right','inner' 使用左边的userid列和右边的userid2列作为连接键即userid=userid2 根据某列的不同的值,创建出另一对应值的列,可用merge方法,连接两个df concat 直接拼接合并 dfs=[pd1,pd2,pd3] datas=pd.concat(dfs,axis=1) axis为1时,横向连接 datas.columns 为 ['userid','r','userid2','filialename','username','useraddress','userid','r','userid2','filialename','username','useraddress',] axis为0时,纵向连接 相当于union alldf2=grouped2.reset_index() 将分组后的索引重新设置为数据
list_data=df2.values.tolist() 将dataframe类型转化为list
df_data['recordat'].apply(lambda x:x.strftime('%Y-%m-%d')) 将函数应用于df的某一列column
读取excel详细入参说明
Read an Excel table into a pandas DataFrame
Parameters:io : string, file-like object, pandas ExcelFile, or xlrd workbook.
The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. For instance, a local file could befile://localhost/path/to/workbook.xlsx
sheetname : string, int, mixed list of strings/ints, or None, default 0 表示读取哪几个工作簿,从0开始
Strings are used for sheet names, Integers are used in zero-indexed sheet positions.
Lists of strings/integers are used to request multiple sheets.
Specify None to get all sheets.
str|int -> DataFrame is returned. list|None -> Dict of DataFrames is returned, with keys representing sheets.
Available Cases
Defaults to 0 -> 1st sheet as a DataFrame1 -> 2nd sheet as a DataFrame“Sheet1” -> 1st sheet as a DataFrame[0,1,”Sheet5”] -> 1st, 2nd & 5th sheet as a dictionary of DataFramesNone -> All sheets as a dictionary of DataFramesheader : int, list of ints, default 0 将某一行设置为标题行,计数从0开始,在跳过行之后重新计数。如skiprows=2,header=2,则将取excel中索引(从0开始计数)为4的行为header,即pd.DataFrame的columns值
表示去掉头两行数据以剩下的数据的索引数(从0开始)为2的行作为header
Row (0-indexed) to use for the column labels of the parsed DataFrame. If a list of integers is passed those row positions will be combined into a MultiIndex
skiprows : list-like 从开头起,跳过哪几行,默认值为None,等同于0 .【 如果取值skiprows=2,则将从excel中索引数(从0开始计数)为2(包含2)的row处开始读取】
Rows to skip at the beginning (0-indexed)
skip_footer : int, default 0。 从尾端起,跳过哪几行, 如等于2,则将跳过最后两行以倒数第三行作为最后一行
Rows at the end to skip (0-indexed)
index_col : int, list of ints, default None 将某一列设置为索引,从0开始计数
Column (0-indexed) to use as the row labels of the DataFrame. Pass None if there is no such column. If a list is passed, those columns will be combined into a MultiIndex
converters : dict, default None 以列名为键,函数为值,对该列的值应用该函数,取结果
Dict of functions for converting values in certain columns. Keys can either be integers or column labels, values are functions that take one input argument, the Excel cell content, and return the transformed content.
parse_cols : int or list, default None 解析哪几列,'A:E'表示解析A列到E列(含)
If None then parse all columns,If int then indicates last column to be parsedIf list of ints then indicates list of column numbers to be parsedIf string then indicates comma separated list of column names and column ranges (e.g. “A:E” or “A,C,E:F”)na_values : list-like, default None 列表,如遇到列表中的值,将其读为na
List of additional strings to recognize as NA/NaN
thousands : str, default None
Thousands separator for parsing string columns to numeric. Note that this parameter is only necessary for columns stored as TEXT in Excel, any numeric columns will automatically be parsed, regardless of display format.
keep_default_na : bool, default True
If na_values are specified and keep_default_na is False the default NaN values are overridden, otherwise they’re appended to
verbose : boolean, default False
Indicate number of NA values placed in non-numeric columns
engine: string, default None
If io is not a buffer or path, this must be set to identify io. Acceptable values are None or xlrd
convert_float : boolean, default True
convert integral floats to int (i.e., 1.0 –> 1). If False, all numeric data will be read in as floats: Excel stores all numbers as floats internally
has_index_names : boolean, default None
DEPRECATED: for version 0.17+ index names will be automatically inferred based on index_col. To read Excel output from 0.16.2 and prior that had saved index names, use True.
Returns:parsed : DataFrame or Dict of DataFrames
DataFrame from the passed in Excel file. See notes in sheetname argument for more information on when a Dict of Dataframes is returned.
转载于:https://www.cnblogs.com/Ting-light/p/9296107.html
相关资源:数据结构—成绩单生成器