zoukankan      html  css  js  c++  java
  • 08-pandas嵌套索引

    import numpy as np
    import pandas as pd
    #1.风格1
    df=pd.DataFrame(np.random.rand(4,2),
                    columns=["A","B"],
                    index=[["1","1","2","2"],
                           ["a","b","a","b"]])
    print(df)
    #2.风格2
    mindex=pd.MultiIndex.from_arrays([["a","a","b","b"],[1,2,1,2]])#双层索引对象
    df2=pd.DataFrame(np.random.rand(4,2),
                     columns=(["A","B"]),
                     index=mindex)
    print(df2)
    #3.风格3
    mindex=pd.MultiIndex.from_tuples([("a",1),("a",2),("b",1),("b",2)])#根据元组构建
    df3=pd.DataFrame(np.random.rand(4,2),
                     columns=(["A","B"]),
                     index=mindex)
    print(df3)
    #4.风格4
    mindex=pd.MultiIndex.from_product([["a","b"],[1,2]])#根据元组构建
    df4=pd.DataFrame(np.random.rand(4,2),
                     columns=(["A","B"]),
                     index=mindex)
    print(df4)
    #5.风格5
    mindex=pd.MultiIndex(levels=[["a","b"],[1,2]],
                         codes=[[0,0,1,1],[0,1,0,1]])#a对应00,b对应11
    df4=pd.DataFrame(np.random.rand(4,2),
                     columns=(["A","B"]),
                     index=mindex)
    print(df4)
    #6.
    index=[("beijing",2007),("beijing", 2017),
           ("beijing", 2027),("shenzhen",2007),
           ("shenzhen",2017),("shenzhen",2027)]
    money=np.random.rand(6)
    Mindex=pd.MultiIndex.from_tuples(index)
    city=pd.DataFrame(money,index=Mindex)
    print(city)
    

      

  • 相关阅读:
    NVCC编译器
    BMP文件格式及读写
    Vim 命令 【转】
    NFS文件系统配置 和 GLIBC更新
    cuda vector addition
    hdu4758 Walk Through Squares 自动机+DP
    hdu4722 Good Numbers
    hdu4725 The Shortest Path in Nya Graph
    Anaconda-科学计算的 Python 发行版
    OUC_NewACMer_Personal_#1 题解
  • 原文地址:https://www.cnblogs.com/wcyMiracle/p/12442957.html
Copyright © 2011-2022 走看看