美文网首页
pandas 常用函数-汇总

pandas 常用函数-汇总

作者: 沧海2122537190 | 来源:发表于2022-08-17 11:43 被阅读0次

    常用函数

    创建新DataFrame

    df=pd.DataFrame()

    把单元格内容转成list

    df[column]=df[column].str.split(" \n",expand=False)

    索引

    把索引建为新列

    df["column_name"]=df.index

    更新筛选后的索引

    df.index = range(len(df))

    重设索引

    result = result.reset_index()

    多重索引

    取出第一级索引 :get_level_values(0)

    行两层索引变成一行

    level0 = result.columns.get_level_values(0)
    level1 = result.columns.get_level_values(1)
    result.columns = level0 + '_' + level1

    列相关

    输出列名

    df.columns.values

    修改列名

    df_new=df.rename(columns={'原列名1': '新列名1','原列名2': '新列名2'})

    根据列名返回列序号

    int=df.columns.get_loc(列名) #从1 开始

    无列名选列

    df.columns[序号]

    字段类型

    dtype
    .str.isdigit() 判断是否数值
    输出行数

    行数:df.shape[0]
    列数:df.shape[1]
    矩阵大小:np.shape(df)
    shape 数据维度

    去重

    df.drop_duplicates()

    删除空行

    df.dropna()
    DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)

    排序

    df.sort_values()
    df.sort_index(axis=0) #列名降序
    df.sort_index(axis=1) #列名升序

    图中文乱码

    import matplotlib.pyplot as plt
    plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
    plt.rcParams['axes.unicode_minus']=False #用来正常显示负号

    int与str转化

    df[column]=df[column].apply(str)
    df[column]=df[column].apply(int)

    去\n \r

    df[column]=df[column].apply(lambda x:x.replace('\n', '').replace('\r', ''))

    多值替换

    df.replace({"a1":"new1","a2":"new2"}, inplace = True)

    列里不同的值

    df.column.nunique()
    df.nunique()

    相关文章

      网友评论

          本文标题:pandas 常用函数-汇总

          本文链接:https://www.haomeiwen.com/subject/nbyjictx.html