在数据框中添加 n 个空行

我想在具有这样一个深度列的深度范围上扩展这个数据框:


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,需要根据扩展范围填充上面的3 行depth。


我该怎么做?


慕的地10843
浏览 219回答 3
3回答

qq_遁去的一_1

您可以使用 mergepd.DataFrame({'depth':depth}).merge(df1,how='left')

一只名叫tom的猫

一种简单的方法是设置索引,depth然后使用depth数组重新索引:df1.set_index('depth').reindex(depth).reset_index()    depth      400.0      401.0      402.0      403.0      404.0      405.00     0.8  13.909261  14.581624  15.253988  15.633908  15.991816  16.3497251     0.9   7.758734   8.173803   8.588872   8.833914   9.066159   9.2984032     1.0   3.513627   3.757856   4.002085   4.146499   4.283401   4.4203033     1.1   2.095409   2.223524   2.351638   2.431543   2.507818   2.5840944     1.2   1.628918   1.695623   1.762327   1.798185   1.831721   1.8652575     1.3   0.782643   0.818065   0.853486   0.874350   0.894119   0.9138876     1.4   0.278548   0.300235   0.321922   0.333470   0.344256   0.3550417     1.5   0.160153   0.173674   0.187195   0.192128   0.196415   0.2007028     1.6  -0.155895  -0.145402  -0.134910  -0.130119  -0.125758  -0.1213969     1.7  -0.152373  -0.144456  -0.136539  -0.134795  -0.133516  -0.13223710    1.8  -0.147820  -0.142969  -0.138118  -0.136049  -0.134189  -0.13233011    1.9  -0.023997  -0.022471  -0.020945  -0.019307  -0.017659  -0.01601212    2.0   0.010729   0.010802   0.010875   0.012037  -0.013281   0.01452513    2.1   0.006050   0.006181   0.006313   0.006674   0.007053   0.00743314    2.2   0.002356   0.002641   0.002927   0.003002   0.003061   0.00312015    2.3        NaN        NaN        NaN        NaN        NaN        NaN16    2.4        NaN        NaN        NaN        NaN        NaN        NaN17    2.5        NaN        NaN        NaN        NaN        NaN        NaN

弑天下

使用 combine_first>>> pd.DataFrame({'depth':depth}).combine_first(df1)使用 pd.concat>>> pd.concat([pd.DataFrame({'depth':depth}), df1.iloc[:,1:]], 1)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python