2

3 つの DataFrame があるとします。

import pandas as pd
import numpy as np

cols = ['A','B','C']
index = [1,2,3,4,5]
np.random.seed(42)

apple = pd.DataFrame(np.random.randn(5,3), index=index, columns=cols)
orange = pd.DataFrame(np.random.randn(5,3), index=index, columns=cols)
banana = pd.DataFrame(np.random.randn(5,3), index=index, columns=cols)

In [50]: apple
Out[50]:
          A         B         C
1  0.496714 -0.138264  0.647689
2  1.523030 -0.234153 -0.234137
3  1.579213  0.767435 -0.469474
4  0.542560 -0.463418 -0.465730
5  0.241962 -1.913280 -1.724918

In [51]: orange
Out[51]:
          A         B         C
1 -0.562288 -1.012831  0.314247
2 -0.908024 -1.412304  1.465649
3 -0.225776  0.067528 -1.424748
4 -0.544383  0.110923 -1.150994
5  0.375698 -0.600639 -0.291694

In [52]: banana
Out[52]:
          A         B         C
1 -0.601707  1.852278 -0.013497
2 -1.057711  0.822545 -1.220844
3  0.208864 -1.959670 -1.328186
4  0.196861  0.738467  0.171368
5 -0.115648 -0.301104 -1.478522

同じ列とインデックスを持つ新しいデータフレームを作成する最良/最速/最も簡単な方法は何ですか? つまり、[1,A] の場合、新しいデータフレームの値は 0.496714 になり、[1,B] の場合、値は 1.852278 になります。ありがとう!

4

2 に答える 2

0

を に連結しDataFramesPanelから を使用してみませんPanel.max()か?

すなわち:pd.Panel({'a':apple ,'b':banana,'o';orange}).max(axis=0)

確かに最速ではありませんが、これにより正しいインデックス配置が保証され、後で別の目的で を使用することができますPanel。データは 3D のように見え、3 つのインデックス要素 (cols/index/fruit) があるため、3D データ構造を使用します。

于 2014-10-10T02:36:37.470 に答える