我如何将numpy数组转换为pandas数据帧

时间:2015-04-30 21:38:32

标签: python numpy pandas

如何将numpy数组转换为pandas数据帧?

例如:

test = np.array([[1,2],[2,3]])
test2 = np.array([[2,4],[2,5]])

到此:

pd.DataFrame({'test':[[1,2],[2,3]],
              'test2':[[2,4],[2,5]]})

     test   test2
0  [1, 2]  [2, 4]
1  [2, 3]  [2, 5]

2 个答案:

答案 0 :(得分:4)

虽然你可以使用

In [85]: pd.DataFrame({'test':test.tolist(), 'test2':test2.tolist()})
Out[85]: 
     test   test2
0  [1, 2]  [2, 4]
1  [2, 3]  [2, 5]

NumPy数组上的计算可能比在Pandas DataFrame上进行的等效计算要快得多,Pandas DataFrame的列包含Python列表。

答案 1 :(得分:0)

如果这些numpy数组的长度相同,那么Panel可能更合适:

module MongoidBridge
  extend ActiveSupport::Concern
  module ClassMethods
  ...
  end

  def create_mongo
  end
end