从嵌套列表中删除空数据框

时间:2020-05-17 23:12:21

标签: python pandas

我有一个嵌套的名为ob1的熊猫DataFrames列表,它包含755个元素,其中大多数是填充的,我不希望知道是否有办法从列表中删除空的DataFrames,同时保留其余部分。列表的内容。

DataFrame
(18, 7)
    EventNumber Time    Angle   Pressure    Altitude    Charge  SlantDepth
58781   549929.0    46819.0 3.450747183156458   3.924570375 126583.0    25.9787 3.9316989052321993
58782   549930.0    46819.0 5.568072608016756   3.924570375 126583.0    26.2338 3.9431757673173187
58913   550415.0    46923.0 5.1191372572199505  3.9157295760000004  126709.0    26.4239 3.9314107214812344
58939   550503.0    46941.0 9.532012358693942   3.917095974 126685.0    26.1587 3.9719355667836536
59014   550769.0    46998.0 11.88142579762788   3.9255084989999998  126597.0    25.9652 4.01145069659106
59045   550916.0    47031.0 3.3080004780775645  3.936725199 126543.0    26.0658 3.9432956406492954
59226   551605.0    47178.0 9.907185122945604   3.95286705  126486.0    26.42   4.012705486073964
59707   553569.0    47583.0 6.967453267688375   3.938805387 126494.0    25.8554 3.968109002595288
59823   553994.0    47673.0 12.015383330129469  3.9332378250000004  126556.0    25.6063 4.0213383375006195
60107   555255.0    47955.0 4.33098224381594    3.948319188 126488.0    26.1338 3.959626123804287
60143   555459.0    48000.0 6.491554523052714   3.946412349 126494.0    26.1301 3.971877920393145
60160   555538.0    48016.0 6.084582601171291   3.9482784   126488.0    25.5353 3.9706470315730114
60244   555845.0    48081.0 9.19683204854241    3.9549880260000005  126461.0    26.1454 4.006491053483471
60332   556190.0    48153.0 6.81899990296998    3.948869826 126480.0    25.8406 3.977002387370091
60522   556918.0    48316.0 10.543683937556434  3.9554162999999996  126447.0    26.2136 4.023347790643213
60765   558058.0    48552.0 7.701297611691935   3.9717315   126366.0    25.8501 4.007881933759479
60776   558082.0    48556.0 8.557869515412516   3.9717315   126366.0    26.2204 4.016450379731363
61375   560476.0    49067.0 5.2238281055462545  4.0103577360000005  126064.0    26.1703 4.027083743799547

14
DataFrame
(0, 7)
    EventNumber Time    Angle   Pressure    Altitude    Charge  SlantDepth

1 个答案:

答案 0 :(得分:2)

首先给出您的问题的具体示例。但我认为df.empty可以胜任。

df=pd.DataFrame()
if df.empty:
    print('df is empty')

编辑:

IIUC,我希望这会有所帮助: 创建随机数据框

df1=pd.DataFrame(columns=list('ABCD') )
df2=pd.DataFrame()
df3=pd.DataFrame()
df4=pd.DataFrame()
df6 = pd.DataFrame(np.random.randn(5, 4), columns=list('ABCD'))
df5 = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))

指派他们列出:

lst=[df1, df2, df3, df4, df5, df6]
len(lst) # 6

创建一个函数来检查空的dfs并返回已填充的dfs的列表

def remove_empty_df(l):
    return [e for e in l if len(e.index) != 0]

new_lst=remove_empty_df(lst)

new_lst现在应该只包含两个元素(填充了dfs