Reputation: 39
I have a nested list of pandas DataFrames called ob1 it contains 755 elements some are populated most are not I would like to know if there is a way to removed the empty DataFrames from the list while keeping the rest Here is an example of the content of the list.
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
Upvotes: 1
Views: 121
Reputation: 2810
First of all give a concrete example of your problem. But I think df.empty
can do the job.
df=pd.DataFrame()
if df.empty:
print('df is empty')
EDIT:
IIUC, I hope this helps: create random dataframes
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'))
Assigning them to list:
lst=[df1, df2, df3, df4, df5, df6]
len(lst) # 6
Create a function to check for empty dfs
and return list of populated 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
should now contain only two elements(populated dfs
)
Upvotes: 2