Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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_Multi Index - Fatal编程技术网

Python 熊猫:向多索引表添加新行无效

Python 熊猫:向多索引表添加新行无效,python,pandas,multi-index,Python,Pandas,Multi Index,我有一个奇怪的问题:我试图在我的表中添加一个包含多个索引的新行。然而,即使我完全按照这里的解决方法来做:。他们的示例和解决方案是有效的,但它不适用于我的数据。要么是我做错了什么,要么是有bug import pandas as pd import datetime as dt 他们的代码(有效): 输出: vals Time hsec 2013-02-03 09:00:01 1 45

我有一个奇怪的问题:我试图在我的表中添加一个包含多个索引的新行。然而,即使我完全按照这里的解决方法来做:。他们的示例和解决方案是有效的,但它不适用于我的数据。要么是我做错了什么,要么是有bug

import pandas as pd
import datetime as dt
他们的代码(有效):

输出:

                          vals
Time                hsec      
2013-02-03 09:00:01 1       45
                    25      46

我的代码(不工作):

输出:

                amount
frames classID        
0      0             2
       2             2
1      0             2
       2             2


注意
5
出现在
df.loc[(0,2)]

在我看来,这似乎是熊猫中的一个bug,但在新发布的0.16中,它显然已经修复:

In [9]: pd.__version__
Out[9]: '0.16.0'

In [10]: df.ix[(1,1),:] = 5

In [11]: df
Out[11]:
                amount
frames classID
0      0             2
       2             2
1      0             2
       2             2
       1             5

但我可以确认这在熊猫0.15.2中确实不起作用。如果可以升级到pandas 0.16,我还建议在这种情况下显式使用
loc
(因此它肯定不会返回到位置整数位置)。但请注意,该漏洞也位于pandas 0.16下方的
loc
,我确认该漏洞已在您使用
pip install pandas
获得的版本中修复,即pandas 0.16.0
d = [[0, 0, 2], [0, 2, 2], [1, 0, 2], [1, 2, 2]]
df = pd.DataFrame(d, columns=('frames', 'classID', 'amount'))
df.set_index(['frames', 'classID'], inplace=True)
print df

df.ix[(1,1),:] = 5
print df
                amount
frames classID        
0      0             2
       2             2
1      0             2
       2             2
                amount
frames classID        
0      0             2
       2             5
1      0             2
       2             2
In [9]: pd.__version__
Out[9]: '0.16.0'

In [10]: df.ix[(1,1),:] = 5

In [11]: df
Out[11]:
                amount
frames classID
0      0             2
       2             2
1      0             2
       2             2
       1             5