pandas.DataFrame.set_index

简介:

用于与pandas.DataFrame.reset_index做对比

DataFrame. set_index ( keysdrop=Trueappend=Falseinplace=Falseverify_integrity=False ) [source]

Set the DataFrame index (row labels) using one or more existing columns. By default yields a new object.

Parameters:

keys : column label or list of column labels / arrays

drop : boolean, default True

Delete columns to be used as the new index

append : boolean, default False

Whether to append columns to existing index

inplace : boolean, default False

Modify the DataFrame in place (do not create a new object)

verify_integrity : boolean, 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 : DataFrame

Examples

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

Set the index to become the ‘month’ column:

>>> df.set_index('month')
       sale  year
month
1      55    2012
4      40    2014
7      84    2013
10     31    2014

Create a multi-index using columns ‘year’ and ‘month’:

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

Create a multi-index using a set of values and a column:

>>> df.set_index([[1, 2, 3, 4], 'year'])
         month  sale
   year
1  2012  1      55
2  2014  4      40
3  2013  7      84
4  2014  10     31
目录
相关文章
|
5月前
|
数据处理 索引 Python
Pandas中concat的用法
Pandas中concat的用法
138 1
|
3月前
|
API 索引 Python
【Pandas】已完美解决:AttributeError: ‘DataFrame‘ object has no attribute ‘ix‘
【Pandas】已完美解决:AttributeError: ‘DataFrame‘ object has no attribute ‘ix‘
114 0
|
5月前
|
Python
使用Python pandas的sort_values()方法可按一个或多个列对DataFrame排序
【5月更文挑战第2天】使用Python pandas的sort_values()方法可按一个或多个列对DataFrame排序。示例代码展示了如何按'Name'和'Age'列排序 DataFrame。先按'Name'排序,再按'Age'排序。sort_values()的by参数接受列名列表,ascending参数控制排序顺序(默认升序),inplace参数决定是否直接修改原DataFrame。
206 1
|
存储 数据处理 索引
【如何在 Pandas DataFrame 中插入一列】
【如何在 Pandas DataFrame 中插入一列】
123 0
pandas list\dict 转换为DataFrame
pandas list\dict 转换为DataFrame
pandas list\dict 转换为DataFrame
|
索引 Python
Pandas 的Merge函数详解
在日常工作中,我们可能会从多个数据集中获取数据,并且希望合并两个或多个不同的数据集。这时就可以使用Pandas包中的Merge函数。在本文中,我们将介绍用于合并数据的三个函数
160 1
|
12月前
|
Cloud Native Go Python
解决Pandas KeyError: “None of [Index([...])] are in the [columns]“问题
解决Pandas KeyError: “None of [Index([...])] are in the [columns]“问题
328 0
Pandas: count() 与 value_counts() 对比
Pandas: count() 与 value_counts() 对比
Pandas: count() 与 value_counts() 对比
Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.
Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.
Pandas pd.merge() 报错:ValueError: You are trying to merge on int64 and object columns.
|
索引 Python
Pandas 根据 index 索引选择某些行
Pandas 根据 index 索引选择某些行