在数据框中添加n个空行

时间:2018-09-26 19:45:06

标签: python pandas

我想在具有深度列的深度范围内扩展此数据框:

import numpy as np
import pandas as pd

depth = np.array([0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5])    

df1 = pd.DataFrame({'depth': [0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2],
           '400.0': [13.909261, 7.758734, 3.513627, 2.095409, 1.628918, 0.782643, 0.278548, 0.160153, -0.155895, -0.152373, -0.147820, -0.023997, 0.010729, 0.006050, 0.002356],
           '401.0': [14.581624, 8.173803, 3.757856, 2.223524, 1.695623, 0.818065, 0.300235, 0.173674, -0.145402, -0.144456, -0.142969, -0.022471, 0.010802, 0.006181, 0.002641],
           '402.0': [15.253988, 8.588872, 4.002085, 2.351638, 1.762327, 0.853486, 0.321922, 0.187195, -0.134910, -0.136539, -0.138118, -0.020945, 0.010875, 0.006313, 0.002927],
           '403.0': [15.633908, 8.833914, 4.146499, 2.431543, 1.798185, 0.874350, 0.333470, 0.192128, -0.130119, -0.134795, -0.136049, -0.019307, 0.012037, 0.006674, 0.003002],
           '404.0': [15.991816, 9.066159, 4.283401, 2.507818, 1.831721, 0.894119, 0.344256, 0.196415, -0.125758, -0.133516  , -0.134189, -0.017659, -0.013281,0.007053, 0.003061],
           '405.0': [16.349725, 9.298403, 4.420303, 2.584094, 1.865257, 0.913887, 0.355041, 0.200702, -0.121396, -0.132237, -0.132330, -0.016012, 0.014525, 0.007433, 0.003120]
           })

因此,在这种情况下,我需要在底部处附加三个带有NaN值的行。

类似地,我有一个df2,其深度范围为1.1到2.5,并且需要根据扩展的depth范围填充上部 3行。

我该怎么做?

3 个答案:

答案 0 :(得分:3)

您可以使用merge

pd.DataFrame({'depth':depth}).merge(df1,how='left')

答案 1 :(得分:2)

一种简单的方法是将索引设置为depth,然后使用您的depth数组重新索引:

df1.set_index('depth').reindex(depth).reset_index()


    depth      400.0      401.0      402.0      403.0      404.0      405.0
0     0.8  13.909261  14.581624  15.253988  15.633908  15.991816  16.349725
1     0.9   7.758734   8.173803   8.588872   8.833914   9.066159   9.298403
2     1.0   3.513627   3.757856   4.002085   4.146499   4.283401   4.420303
3     1.1   2.095409   2.223524   2.351638   2.431543   2.507818   2.584094
4     1.2   1.628918   1.695623   1.762327   1.798185   1.831721   1.865257
5     1.3   0.782643   0.818065   0.853486   0.874350   0.894119   0.913887
6     1.4   0.278548   0.300235   0.321922   0.333470   0.344256   0.355041
7     1.5   0.160153   0.173674   0.187195   0.192128   0.196415   0.200702
8     1.6  -0.155895  -0.145402  -0.134910  -0.130119  -0.125758  -0.121396
9     1.7  -0.152373  -0.144456  -0.136539  -0.134795  -0.133516  -0.132237
10    1.8  -0.147820  -0.142969  -0.138118  -0.136049  -0.134189  -0.132330
11    1.9  -0.023997  -0.022471  -0.020945  -0.019307  -0.017659  -0.016012
12    2.0   0.010729   0.010802   0.010875   0.012037  -0.013281   0.014525
13    2.1   0.006050   0.006181   0.006313   0.006674   0.007053   0.007433
14    2.2   0.002356   0.002641   0.002927   0.003002   0.003061   0.003120
15    2.3        NaN        NaN        NaN        NaN        NaN        NaN
16    2.4        NaN        NaN        NaN        NaN        NaN        NaN
17    2.5        NaN        NaN        NaN        NaN        NaN        NaN

答案 2 :(得分:2)

使用combine_first

>>> pd.DataFrame({'depth':depth}).combine_first(df1)

使用pd.concat

>>> pd.concat([pd.DataFrame({'depth':depth}), df1.iloc[:,1:]], 1)