从DataFrame列表中删除DataFrame,具体取决于DataFrame-python?

时间:2019-04-11 15:21:06

标签: python dataframe

我有一个DataFrames列表,我想从满足以下任何条件的列表中删除DataFrames:

  • 如果DataFrame的列少于2个。
  • 如果DataFrame在任何地方都包含字符串“ A3”。

我为列长尝试的代码如下所示,该列表名为df_list:

for i in df_list:
   if len(i.columns) == 1:
      del[i]

df_list = [i for i in df_list if not (i.shape[1] == 2)]

我尝试删除包含字符串'A3'的DataFrame的代码是:

df_list = [i for i in df_list if not ('A3' in i.columns)]

我知道我的电话号码有误,但也没有在应该的时候从我的清单中删除任何东西,有人知道正确的方法吗?

2 个答案:

答案 0 :(得分:1)

这是您要找的吗?

import pandas as pd

url = 'https://www.bls.gov/web/empsit/cesbmart.htm'
df_list = pd.read_html(url)


key_word = 'CES'

delete_by_idx = []
for idx, dataframe in enumerate(df_list):
    A3_found = False

    # Check if A3 is in any row
    for i, row in dataframe.iterrows():
        if row.str.contains(key_word).any():
           A3_found = True

    # If A3 was found, delete the dataframe
    if A3_found == True:
        delete_by_idx.append(idx)  
        continue

    # If A3 is in the columns, delete the dataframe
    cols =  [ str(col_name) for col_name in list(dataframe.columns) ]
    if any(key_word in x for x in cols):
        delete_by_idx.append(idx) 
        continue

    # If columns less than or equal to 2, delete the dataframe
    if len(dataframe.columns) <= 2:
        delete_by_idx.append(idx) 
        continue

delete_by_idx.sort(reverse=True)
for each in delete_by_idx:        
    del df_list[each] 

答案 1 :(得分:0)

这将检查列名称中的“ A3”。然后,您可以使用相同的格式来检查列的值。

id