pandas.DataFrame.set_index的使用介绍

官方参考链接:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html#pandas.DataFrame.set_index

Set the DataFrame index using existing columns.

Set the DataFrame index (row labels) using one or more existing columns or arrays (of the correct length). The index can replace the existing index or expand on it.

Parameters
keyslabel or array-like or list of labels/arrays

This parameter can be either a single column key, a single array of the same length as the calling DataFrame, or a list containing an arbitrary combination of column keys and arrays. Here, “array” encompasses SeriesIndexnp.ndarray, and instances of Iterator.

dropbool, default True

Delete columns to be used as the new index.

appendbool, default False

Whether to append columns to existing index.

inplacebool, default False

If True, modifies the DataFrame in place (do not create a new object).

verify_integritybool, default False

Check the new index for duplicates. Otherwise defer the check until necessary. Setting to False will improve the performance of this method.

Returns
DataFrame or None

Changed row labels or None if inplace=True.

个人理解:

这是一个设置index的命令,主要参数为keys. 这个参数可以式已经存在的df对象中的columns的名称,也可以是一个单独的数组对象,数组对象包含SeriesIndexnp.ndarray, and instances of Iterator.

drop :表示为是否丢弃设置为index的columns   bool值,默认为true。

append: 是否为添加的索引,默认为flase,true会与源索引变成组合索引。

verify_integrity:检查新索引是否有重复项,默认为false。

官方代码实操学习

常规操作,设置一个列为index

In [32]: df = pd.DataFrame({'month': [1, 4, 7, 10], 
    ...:                    'year': [2012, 2014, 2013, 2014], 
    ...:                    'sale': [55, 40, 84, 31]})                                                      

In [33]: df                                                                                                 
Out[33]: 
   month  year  sale
0      1  2012    55
1      4  2014    40
2      7  2013    84
3     10  2014    31

In [34]: df.set_index('month')                                                                              
Out[34]: 
       year  sale
month            
1      2012    55
4      2014    40
7      2013    84
10     2014    31

  

设置append为True,组合为联合索引。

In [35]: df.set_index('year',append=True)                                                                   
Out[35]: 
        month  sale
  year             
0 2012      1    55
1 2014      4    40
2 2013      7    84
3 2014     10    31

  

当然也可以通过设置多列,设置组合索引。

In [37]: df.set_index(['year','month'])                                                                     
Out[37]: 
            sale
year month      
2012 1        55
2014 4        40
2013 7        84
2014 10       31

In [38]:                                                                                                    

In [38]: df.set_index([pd.Index([2,3,4,5]),'year'])                                                         
Out[38]: 
        month  sale
  year             
2 2012      1    55
3 2014      4    40
4 2013      7    84
5 2014     10    31

  最后也可以设置外部传入的可迭代对象为index

In [39]: new_index = list('abcd')                                                                           

In [40]: df.set_index([new_index])                                                                          
Out[40]: 
   month  year  sale
a      1  2012    55
b      4  2014    40
c      7  2013    84
d     10  2014    31

  

原文地址:https://www.cnblogs.com/sidianok/p/14365948.html