我有一个由pandas.read_csv(...)
我还添加了一列用于索引:da.index = pd.date_range(...)
所以我的2D数组现在大小为6x17511。
我想在这个2D数组中插入/追加一行,怎么做?
我已经尝试过:np.insert(da,1,np.array((1,2,3,4,5,6)), 0)
但它说:
ValueError:传递值的形状为(6,17512),指数暗示(6, 17511)
提前致谢!
答案 0 :(得分:0)
我认为这是一个愚蠢的问题而不是熊猫问题......
你可以试试vstack ......
import numpy as np
da = np.random.rand(17511, 6)
newrow = np.array((1,2,3,4,5,6))
da = np.vstack([da, newrow])
哪个收益......
In [5]: da
Out[5]:
array([[ 0.50203777, 0.55102172, 0.74798053, 0.57291239, 0.38977322,
0.40878739],
[ 0.9960413 , 0.22293403, 0.34136638, 0.12845067, 0.20262593,
0.50798698],
[ 0.05298782, 0.09129754, 0.40833606, 0.67150583, 0.19569471,
0.75176924],
...,
[ 0.97927055, 0.44649323, 0.84851791, 0.05370892, 0.94375771,
0.24508979],
[ 0.85952039, 0.2852414 , 0.85662827, 0.97665465, 0.65528357,
0.71483845],
[ 1. , 2. , 3. , 4. , 5. ,
6. ]])
In [6]: len(da)
Out[6]: 17512
并且(虽然有不同的随机数),我可以按如下方式访问numpy数组的顶部和底部......
In [9]: da[:5]
Out[9]:
array([[ 0.76697236, 0.96475768, 0.09145486, 0.27159858, 0.05160006,
0.66495098],
[ 0.62635043, 0.1316334 , 0.66257157, 0.99141318, 0.77212699,
0.17016979],
[ 0.86705298, 0.11120927, 0.29585339, 0.44128326, 0.32290492,
0.99298705],
[ 0.74053894, 0.90743885, 0.99838398, 0.40713677, 0.17337202,
0.56982539],
[ 0.99136919, 0.13045787, 0.67881652, 0.03814385, 0.98036307,
0.53594215]])
In [10]: da[-5:]
Out[10]:
array([[ 0.8793664 , 0.0392912 , 0.8106504 , 0.17920025, 0.26767578,
0.98386519],
[ 0.41231276, 0.02633723, 0.7872108 , 0.60894162, 0.5358851 ,
0.65758067],
[ 0.10341791, 0.48079533, 0.1638601 , 0.5470736 , 0.7339205 ,
0.60609949],
[ 0.55320512, 0.12962241, 0.84443947, 0.81012583, 0.22057856,
0.33495709],
[ 1. , 2. , 3. , 4. , 5. ,
6. ]])