Python 如何更改 Pandas 数据框索引值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14110721/
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
How to change Pandas dataframe index value?
提问by bigbug
I have a df:
我有一个df:
>>> df
sales cash
STK_ID RPT_Date
000568 20120930 80.093 57.488
000596 20120930 32.585 26.177
000799 20120930 14.784 8.157
And want to change first row's index value from ('000568','20120930')to ('000999','20121231'). Final result will be:
并希望将第一行的索引值从 更改('000568','20120930')为('000999','20121231')。最终结果将是:
>>> df
sales cash
STK_ID RPT_Date
000999 20121231 80.093 57.488
000596 20120930 32.585 26.177
000799 20120930 14.784 8.157
How to achieve this?
如何实现这一目标?
采纳答案by unutbu
With this setup:
使用此设置:
import pandas as pd
import io
text = '''\
STK_ID RPT_Date sales cash
000568 20120930 80.093 57.488
000596 20120930 32.585 26.177
000799 20120930 14.784 8.157
'''
df = pd.read_csv(io.BytesIO(text), delimiter = ' ',
converters = {0:str})
df.set_index(['STK_ID','RPT_Date'], inplace = True)
The index, df.indexcan be reassigned to a new MultiIndexlike this:
索引,df.index可以MultiIndex像这样重新分配给一个新的:
index = df.index
names = index.names
index = [('000999','20121231')] + df.index.tolist()[1:]
df.index = pd.MultiIndex.from_tuples(index, names = names)
print(df)
# sales cash
# STK_ID RPT_Date
# 000999 20121231 80.093 57.488
# 000596 20120930 32.585 26.177
# 000799 20120930 14.784 8.157
Or, the index could be made into columns, the values in the columns could be then reassigned, and then the columns returned to indices:
或者,可以将索引制成列,然后可以重新分配列中的值,然后将列返回到索引:
df.reset_index(inplace = True)
df.ix[0, ['STK_ID', 'RPT_Date']] = ('000999','20121231')
df = df.set_index(['STK_ID','RPT_Date'])
print(df)
# sales cash
# STK_ID RPT_Date
# 000999 20121231 80.093 57.488
# 000596 20120930 32.585 26.177
# 000799 20120930 14.784 8.157
Benchmarking with IPython %timeitsuggests reassigning the index (the first method, above) is significantly faster than resetting the index, modifying column values, and then setting the index again (the second method, above):
使用 IPython 进行基准测试%timeit表明,重新分配索引(上面的第一种方法)比重置索引、修改列值,然后再次设置索引(上面的第二种方法)要快得多:
In [2]: %timeit reassign_index(df)
10000 loops, best of 3: 158 us per loop
In [3]: %timeit reassign_columns(df)
1000 loops, best of 3: 843 us per loop

