Python 重命名 Pandas DataFrame 索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19851005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rename Pandas DataFrame Index
提问by Mattijn
I've a csv file without header, with a DateTime index. I want to rename the index and column name, but with df.rename() only the column name is renamed. Bug? I'm on version 0.12.0
我有一个没有标题的 csv 文件,带有 DateTime 索引。我想重命名索引和列名,但使用 df.rename() 只重命名列名。漏洞?我在 0.12.0 版本
In [2]: df = pd.read_csv(r'D:\Data\DataTimeSeries_csv//seriesSM.csv', header=None, parse_dates=[[0]], index_col=[0] )
In [3]: df.head()
Out[3]:
1
0
2002-06-18 0.112000
2002-06-22 0.190333
2002-06-26 0.134000
2002-06-30 0.093000
2002-07-04 0.098667
In [4]: df.rename(index={0:'Date'}, columns={1:'SM'}, inplace=True)
In [5]: df.head()
Out[5]:
SM
0
2002-06-18 0.112000
2002-06-22 0.190333
2002-06-26 0.134000
2002-06-30 0.093000
2002-07-04 0.098667
采纳答案by Andy Hayden
The rename
method takes a dictionary for the index which applies to index values.
You want to rename to index level's name:
该rename
方法采用适用于索引值的索引字典。
您想重命名为索引级别的名称:
df.index.names = ['Date']
A good way to think about this is that columns and index are the same type of object (Index
or MultiIndex
), and you can interchange the two via transpose.
考虑这一点的一个好方法是,列和索引是相同类型的对象(Index
或MultiIndex
),您可以通过转置来交换两者。
This is a little bit confusing since the index names have a similar meaning to columns, so here are some more examples:
这有点令人困惑,因为索引名称与列的含义相似,因此这里有更多示例:
In [1]: df = pd.DataFrame([[1, 2, 3], [4, 5 ,6]], columns=list('ABC'))
In [2]: df
Out[2]:
A B C
0 1 2 3
1 4 5 6
In [3]: df1 = df.set_index('A')
In [4]: df1
Out[4]:
B C
A
1 2 3
4 5 6
You can see the rename on the index, which can change the value1:
您可以在索引上看到重命名,可以将值更改为1:
In [5]: df1.rename(index={1: 'a'})
Out[5]:
B C
A
a 2 3
4 5 6
In [6]: df1.rename(columns={'B': 'BB'})
Out[6]:
BB C
A
1 2 3
4 5 6
Whilst renaming the level names:
重命名关卡名称时:
In [7]: df1.index.names = ['index']
df1.columns.names = ['column']
Note: this attribute is just a list, and you could do the renaming as a list comprehension/map.
注意:此属性只是一个列表,您可以将重命名为列表理解/映射。
In [8]: df1
Out[8]:
column B C
index
1 2 3
4 5 6
回答by David Smith
In Pandas version 0.13 and greater the index level names are immutable (type FrozenList
) and can no longer be set directly. You must first use Index.rename()
to apply the new index level names to the Index and then use DataFrame.reindex()
to apply the new index to the DataFrame. Examples:
在 Pandas 0.13 及更高版本中,索引级别名称是不可变的(类型FrozenList
),不能再直接设置。您必须首先使用Index.rename()
将新索引级别名称应用于索引,然后使用DataFrame.reindex()
将新索引应用于数据帧。例子:
For Pandas version < 0.13
对于 Pandas 版本 < 0.13
df.index.names = ['Date']
For Pandas version >= 0.13
对于 Pandas 版本 >= 0.13
df = df.reindex(df.index.rename(['Date']))
回答by LondonRob
You can also use Index.set_names
as follows:
您还可以Index.set_names
按如下方式使用:
In [25]: x = pd.DataFrame({'year':[1,1,1,1,2,2,2,2],
....: 'country':['A','A','B','B','A','A','B','B'],
....: 'prod':[1,2,1,2,1,2,1,2],
....: 'val':[10,20,15,25,20,30,25,35]})
In [26]: x = x.set_index(['year','country','prod']).squeeze()
In [27]: x
Out[27]:
year country prod
1 A 1 10
2 20
B 1 15
2 25
2 A 1 20
2 30
B 1 25
2 35
Name: val, dtype: int64
In [28]: x.index = x.index.set_names('foo', level=1)
In [29]: x
Out[29]:
year foo prod
1 A 1 10
2 20
B 1 15
2 25
2 A 1 20
2 30
B 1 25
2 35
Name: val, dtype: int64
回答by danio
If you want to use the same mapping for renaming both columns and index you can do:
如果您想使用相同的映射来重命名列和索引,您可以执行以下操作:
mapping = {0:'Date', 1:'SM'}
df.index.names = list(map(lambda name: mapping.get(name, name), df.index.names))
df.rename(columns=mapping, inplace=True)
回答by Serge Stroobandt
For newer pandas
versions
对于较新的pandas
版本
df.index = df.index.rename('new name')
or
或者
df.index.rename('new name', inplace=True)
The latter is requiredif a data frame should retain all its properties.
如果数据框应保留其所有属性,则需要后者。
回答by Ted Petrou
The currently selected answer does not mention the rename_axis
method which can be used to rename the index and column levels.
当前选择的答案没有提到rename_axis
可用于重命名索引和列级别的方法。
Pandas has some quirkiness when it comes to renaming the levels of the index. There is also a new DataFrame method rename_axis
available to change the index level names.
在重命名索引级别时,Pandas 有一些古怪之处。还有一个新的 DataFrame 方法rename_axis
可用于更改索引级别名称。
Let's take a look at a DataFrame
我们来看一个DataFrame
df = pd.DataFrame({'age':[30, 2, 12],
'color':['blue', 'green', 'red'],
'food':['Steak', 'Lamb', 'Mango'],
'height':[165, 70, 120],
'score':[4.6, 8.3, 9.0],
'state':['NY', 'TX', 'FL']},
index = ['Jane', 'Nick', 'Aaron'])
This DataFrame has one level for each of the row and column indexes. Both the row and column index have no name. Let's change the row index level name to 'names'.
此 DataFrame 为每个行和列索引都有一个级别。行和列索引都没有名称。让我们将行索引级别名称更改为“名称”。
df.rename_axis('names')
The rename_axis
method also has the ability to change the column level names by changing the axis
parameter:
该rename_axis
方法还可以通过更改axis
参数来更改列级别名称:
df.rename_axis('names').rename_axis('attributes', axis='columns')
If you set the index with some of the columns, then the column name will become the new index level name. Let's append to index levels to our original DataFrame:
如果使用某些列设置索引,则列名将成为新的索引级别名称。让我们将索引级别附加到原始 DataFrame 中:
df1 = df.set_index(['state', 'color'], append=True)
df1
Notice how the original index has no name. We can still use rename_axis
but need to pass it a list the same length as the number of index levels.
请注意原始索引如何没有名称。我们仍然可以使用,rename_axis
但需要向它传递一个长度与索引级别数相同的列表。
df1.rename_axis(['names', None, 'Colors'])
You can use None
to effectively delete the index level names.
您可以使用None
来有效地删除索引级别名称。
Series work similarly but with some differences
系列工作类似但有一些差异
Let's create a Series with three index levels
让我们创建一个具有三个索引级别的系列
s = df.set_index(['state', 'color'], append=True)['food']
s
state color
Jane NY blue Steak
Nick TX green Lamb
Aaron FL red Mango
Name: food, dtype: object
We can use rename_axis
similarly to how we did with DataFrames
我们可以rename_axis
像使用 DataFrames 一样使用
s.rename_axis(['Names','States','Colors'])
Names States Colors
Jane NY blue Steak
Nick TX green Lamb
Aaron FL red Mango
Name: food, dtype: object
Notice that the there is an extra piece of metadata below the Series called Name
. When creating a Series from a DataFrame, this attribute is set to the column name.
请注意,在名为Name
. 从 DataFrame 创建系列时,此属性设置为列名称。
We can pass a string name to the rename
method to change it
我们可以将字符串名称传递给rename
方法来更改它
s.rename('FOOOOOD')
state color
Jane NY blue Steak
Nick TX green Lamb
Aaron FL red Mango
Name: FOOOOOD, dtype: object
DataFrames do not have this attribute and infact will raise an exception if used like this
DataFrames 没有这个属性,如果像这样使用,实际上会引发异常
df.rename('my dataframe')
TypeError: 'str' object is not callable
Prior to pandas 0.21, you could have used rename_axis
to rename the values in the index and columns. It has been deprecated so don't do this
在 pandas 0.21 之前,您可以使用rename_axis
重命名索引和列中的值。它已被弃用,所以不要这样做
回答by Jan H.
df.index.rename('new name', inplace=True)
Is the only one that does the job for me (pandas 0.22.0).
Without the inplace=True, the name of the index is not set in my case.
是唯一为我做这项工作的人(熊猫 0.22.0)。
如果没有 inplace=True,在我的情况下没有设置索引的名称。
回答by nucsit026
you can use index
and columns
attributes of pandas.DataFrame
. NOTE: number of elements of list must match the number of rows/columns.
您可以使用index
和 的columns
属性pandas.DataFrame
。注意:列表的元素数必须与行/列数匹配。
# A B C
# ONE 11 12 13
# TWO 21 22 23
# THREE 31 32 33
df.index = [1, 2, 3]
df.columns = ['a', 'b', 'c']
print(df)
# a b c
# 1 11 12 13
# 2 21 22 23
# 3 31 32 33