zoukankan      html  css  js  c++  java
  • pandas.MultiIndex

    分层/多级索引能在较低纬度的数据结构(如Series和DataFrame)中存储和操作任意维度的数据,

    1. 创建MultiIndex

    MultiIndex对象是标准索引Index对象的扩展,可以将MultiIndex看作一个元组数组,其中每个元组都是唯一的。可以从数组列表(MultiIndex.from_arrays())、元组数组(MultiIndex.from_tuples())、交叉迭代器集(MultiIndex.from_product())或DaTaFrame(使用using MultiIndex.from_frame)创建多索引。当传递一个元组列表时,索引构造函数将尝试返回一个MultiIndex。

    1. from_tuples

    先创建一个元组构成的列表

    import pandas as pd
    
    arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
                ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
    
    tuples = list(zip(*arrays))
    print(tuples)
    [('bar', 'one'), ('bar', 'two'), ('baz', 'one'), ('baz', 'two'), ('foo', 'one'), ('foo', 'two'), ('qux', 'one'), ('qux', 'two')]
    

    将元组列表转换为MultiIndex:

    MultiIndex.from_tuples(tuplessortorder=Nonenames=None)

    index = pd.MultiIndex.from_tuples(tuples, names=('first', 'second'))
    index
    MultiIndex(levels=[['bar', 'baz', 'foo', 'qux'], ['one', 'two']],
               labels=[[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]],
               names=['first', 'second'])

    创建一个Series,并设置它的index:

    import numpy as np
    
    s = pd.Series(np.random.randn(8), index=index)
    s
    first  second
    bar    one      -1.219790
           two      -1.299911
    baz    one      -0.707801
           two       0.280277
    foo    one       0.683006
           two       1.279083
    qux    one      -0.659377
           two       0.095253
    dtype: float64

    创建一个DataFrame,并设置它的index:

    df=pd.DataFrame(np.random.randint(1,10,(8, 5)),index=index)
    df

    2. from_arrays

    如果说from_tuples接受的参数是“行”的列表,那么from_arrays接受的参数就是“列”的列表

    MultiIndex.from_arrays(arrayssortorder=Nonenames=None)

    arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
                ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
    
    index = pd.MultiIndex.from_arrays(arrays)
    s = pd.Series(np.random.randn(8), index=index)
    s
    bar  one    0.817429
         two    0.248518
    baz  one   -0.684833
         two    0.437428
    foo  one   -0.019753
         two   -1.035943
    qux  one    1.602173
         two   -1.592012
    dtype: float64

    为了方便,通常可以直接在Series的构造函数中使用:

    arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
                ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
    
    s = pd.Series(np.random.randn(8), index=arrays)
    s
    bar  one   -0.674630
         two   -0.823605
    baz  one    0.553978
         two   -1.315951
    foo  one    1.318207
         two   -3.419469
    qux  one   -0.618415
         two    1.216639
    dtype: float64

    3. from_product

    假如有两个list,这两个list内的元素相互交叉,两两搭配,这就是两个list的product:

    MultiIndex.from_product(iterablessortorder=Nonenames=None)

    lists = [['bar', 'baz', 'foo', 'qux'], ['one', 'two']]
    
    index = pd.MultiIndex.from_product(lists, names=['first', 'second'])
    s = pd.Series(np.random.randn(len(index)), index=index)
    s
    first  second
    bar    one       1.131558
           two      -2.186842
    baz    one      -0.045946
           two       1.376054
    foo    one       1.384699
           two      -0.141007
    qux    one      -0.474400
           two       1.402611
    dtype: float64

    2. 特征选取作为pipeline(管道)的一部分

    特征选择通常在实际的学习之前用来做预处理。在scikit-learn中推荐的方式是使用:sklearn.pipeline.Pipeline

    clf = Pipeline([
      ('feature_selection', SelectFromModel(LinearSVC(penalty="l1"))),
      ('classification', RandomForestClassifier())
    ])
    clf.fit(X, y)

    在这段代码中,利用 sklearn.svm.LinearSVC 和 sklearn.feature_selection.SelectFromModel 来评估特征的重要性并选择相互相关的特征。

    来自:Python开发最佳实践

  • 相关阅读:
    自动化测试selenium教程
    Java开发.gitignore文件包含.iml,.log的看法
    基于接口设计与编程
    搭建大众点评CAT监控平台
    正确的打日志姿势
    【每天一条Linux指令-Day1】kill掉多个mysql的进程
    一道SQL面试题——表行列数据转换(表转置)
    @SuppressWarnings注解用法详解
    Spring IoC的底层技术支持——Java反射机制
    出现java.lang.NoSuchMethodError错误的原因
  • 原文地址:https://www.cnblogs.com/keye/p/13354806.html
Copyright © 2011-2022 走看看