Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更改数据帧索引值?_Python_Pandas - Fatal编程技术网

Python 如何更改数据帧索引值?

Python 如何更改数据帧索引值?,python,pandas,Python,Pandas,我有一个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 并希望将第一行的索引值从('000568','20120930')更改为('000999','20121231

我有一个
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
并希望将第一行的索引值从
('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
如何实现这一点?

使用此设置:

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)
可以将索引
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
或者,可以将索引制成列,然后重新分配列中的值,然后将列返回到索引:

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

使用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