6

以下の形式のマルチインデックス データフレームがあります。データフレーム内のすべての値を df['three'] で割るにはどうすればよいですか?

          one                  two                three              
Number      1      2      3      1      2      3      1      2      3
Name                                                                 
grethe -0.299 -1.444 -0.920  1.378  0.376 -0.396  0.518 -0.816 -0.329
hans    0.493  1.183 -0.741 -0.267 -0.564  0.281  1.550  0.544 -0.892

これを試してみると、

>>> df.div(df['three'])

またはこれ

>>> df / df['three']

次のエラーが表示されます。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 218, in f
    return self._combine_frame(other, na_op, fill_value, level)
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 3819, in _combine_frame
    this, other = self.align(other, join='outer', level=level, copy=False)
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 2490, in align
    fill_axis=fill_axis)
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 2521, in _align_frame
    fill_value=fill_value)
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 2732, in _reindex_with_indexers
    fill_value=fill_value)
  File "C:\Anaconda\lib\site-packages\pandas\core\internals.py", line 1976, in reindex_indexer
    return self._reindex_indexer_items(new_axis, indexer, fill_value)
  File "C:\Anaconda\lib\site-packages\pandas\core\internals.py", line 2020, in _reindex_indexer_items
    return BlockManager(new_blocks, new_axes)
  File "C:\Anaconda\lib\site-packages\pandas\core\internals.py", line 1007, in __init__
    self._set_ref_locs(do_refs=True)
  File "C:\Anaconda\lib\site-packages\pandas\core\internals.py", line 1117, in _set_ref_locs
    "does not have _ref_locs set" % (block,labels))
AssertionError: cannot create BlockManager._ref_locs because block [FloatBlock: [1, 2, 3, one, one, one, three, three, three, two, two, two], 12 x 2, dtype float64] with duplicate items [Index([u'1', u'2', u'3', u'one', u'one', u'one', u'three', u'three', u'three', u'two', u'two', u'two'], dtype=object)] does not have _ref_locs set

私もこのように積み重ねてみましたが、うまくいきませんでした。

>>> df.stack().div(df.stack()['three']).unstack()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 220, in f
    return self._combine_series(other, na_op, fill_value, axis, level)
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 3860, in _combine_series
    return self._combine_match_columns(other, func, fill_value)
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 3893, in _combine_match_columns
    left, right = self.align(other, join='outer', axis=1, copy=False)
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 2495, in align
    fill_axis=fill_axis)
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 2562, in _align_series
    right_result = other if ridx is None else other.reindex(join_index)
  File "C:\Anaconda\lib\site-packages\pandas\core\series.py", line 2643, in reindex
    takeable=takeable)
  File "C:\Anaconda\lib\site-packages\pandas\core\index.py", line 2178, in reindex
    target = MultiIndex.from_tuples(target)
  File "C:\Anaconda\lib\site-packages\pandas\core\index.py", line 1799, in from_tuples
    arrays = list(lib.tuples_to_object_array(tuples).T)
  File "inference.pyx", line 914, in pandas.lib.tuples_to_object_array (pandas\lib.c:43497)
TypeError: Expected tuple, got str
4

1 に答える 1