pandas 向 MultiIndex DataFrame/Series 添加一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24917700/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:17:09  来源:igfitidea点击:

adding a row to a MultiIndex DataFrame/Series

pythonpandasindexing

提问by user3820991

I was wondering if there is an equivalent way to add a row to a Series or DataFrame with a MultiIndex as there is with a single index, i.e. using .ix or .loc?

我想知道是否有一种等效的方法可以像使用单个索引(即使用 .ix 或 .loc)那样使用 MultiIndex 将行添加到 Series 或 DataFrame 中?

I thought the natural way would be something like

我认为自然的方式会是这样的

row_to_add = pd.MultiIndex.from_tuples()
df.ix[row_to_add] = my_row

but that raises a KeyError. I know I can use .append(), but I would find it much neater to use .ix[] or .loc[].

但这会引发 KeyError。我知道我可以使用 .append(),但我会发现使用 .ix[] 或 .loc[] 会更简洁。

here an example:

这里有一个例子:

>>> df = pd.DataFrame({'Time': [dt.datetime(2013,2,3,9,0,1), dt.datetime(2013,2,3,9,0,1)], 'hsec': [1,25], 'vals': [45,46]})
>>> df
                 Time  hsec  vals
0 2013-02-03 09:00:01     1    45
1 2013-02-03 09:00:01    25    46

[2 rows x 3 columns]
>>> df.set_index(['Time','hsec'],inplace=True)
>>> ind = pd.MultiIndex.from_tuples([(dt.datetime(2013,2,3,9,0,2),0)],names=['Time','hsec'])
>>> df.ix[ind] = 5

Traceback (most recent call last):
  File "<pyshell#201>", line 1, in <module>
    df.ix[ind] = 5
  File "C:\Program Files\Python27\lib\site-packages\pandas\core\indexing.py", line 96, in __setitem__
    indexer = self._convert_to_indexer(key, is_setter=True)
  File "C:\Program Files\Python27\lib\site-packages\pandas\core\indexing.py", line 967, in _convert_to_indexer
    raise KeyError('%s not in index' % objarr[mask])
KeyError: "[(Timestamp('2013-02-03 09:00:02', tz=None), 0L)] not in index"

回答by Jeff

You have to specify a tuple for the multi-indexing to work (AND you have to fully specify all axes, e.g. the :is necessary)

您必须为多索引指定一个元组才能工作(并且您必须完全指定所有轴,例如:是必要的)

In [26]: df.ix[(dt.datetime(2013,2,3,9,0,2),0),:] = 5

In [27]: df
Out[27]: 
                          vals
Time                hsec      
2013-02-03 09:00:01 1       45
                    25      46
2013-02-03 09:00:02 0        5

Easier to reindex and/or concat/append a new dataframe though. Generally setting (with this kind of enlargement), only makes sense if you are doing it with a small number of values. As this makes a copy when you do this.

不过更容易重新索引和/或连接/附加新的数据帧。一般设置(使用这种放大),仅当您使用少量值进行设置时才有意义。因为这会在您执行此操作时进行复制。

回答by user3820991

Update since .ixis depreciated: Today you could do:

更新因为.ix折旧:今天你可以这样做:

# say you have dataframe x
x
Out[78]: 
              a    b       time
indA indB                     
a    i      0.0  NaN 2018-09-12
b    j      1.0  2.0 2018-10-12
c    k      2.0  3.0 2018-11-12
     f      NaN  NaN        NaT
d    i      5.0  NaN        NaT

x.loc[('a','k'),:] = (3.5,6,pd.NaT)

x
Out[80]: 
              a    b       time
indA indB                     
a    i      0.0  NaN 2018-09-12
b    j      1.0  2.0 2018-10-12
c    k      2.0  3.0 2018-11-12
     f      NaN  NaN        NaT
d    i      5.0  NaN        NaT
a    k      3.5  6.0        NaT