Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除行值类似于“[]”的数据帧列_Python_Pandas_Dataframe - Fatal编程技术网

Python 删除行值类似于“[]”的数据帧列

Python 删除行值类似于“[]”的数据帧列,python,pandas,dataframe,Python,Pandas,Dataframe,在我的dataframe中,有几行的值类似于[],我想删除整个列,如何完成 这是我的密码 import re for i, row in df.iterrows(): for j, column in row.iteritems(): #print(column) #print(j) t = re.findall("\[\]", column) if t: df.drop(j, axis=1, inplace=True)

在我的dataframe中,有几行的值类似于[],我想删除整个列,如何完成

这是我的密码

import re

for i, row in df.iterrows():
    for j, column in row.iteritems():
      #print(column)
      #print(j)
      t = re.findall("\[\]", column)
      if t:
        df.drop(j, axis=1, inplace=True)
      else:
        print('Nothing'+j)
这是我得到的错误

TypeError                                 Traceback (most recent call last)
<ipython-input-72-927572386a36> in <module>
      3     #print(column)
      4     #print(j)
----> 5         t = re.findall("\[\]", column)
      6         if t:
      7             df.drop(j, axis=1, inplace=True)

/anaconda3/lib/python3.7/re.py in findall(pattern, string, flags)
    221 
    222     Empty matches are included in the result."""
--> 223     return _compile(pattern, flags).findall(string)
    224 
    225 def finditer(pattern, string, flags=0):

TypeError: expected string or bytes-like object

我相信您需要将强制转换值转换为布尔值,并仅过滤带有以下内容的Trues列:

详情:


我相信您需要将强制转换值转换为布尔值,并仅过滤带有以下内容的Trues列:

详情:


如果列中的所有值均为[]或列中的至少一个值[],是否需要删除列?如果列中的所有值均为[]或列中的至少一个值[],是否需要删除列?至少一个值Hi-jezrael,是否可以解释这是如何工作的?df.astypebool将数据帧转换为bool。空列表将转换为False。然后df.loc[:,df.astypebool.all]选择所有值都不为false的所有列Hi jezrael,您能解释一下这是如何工作的吗?df.astypebool将数据帧转换为bool。空列表将转换为False。然后df.loc[:,df.astypebool.all]选择所有值都不为False的列
df = pd.DataFrame({'a':[[], [], ['d']],
                   'b':[['d'], ['a'], ['d']],
                   'c':[[], [], []]})

print (df)
     a    b   c
0   []  [d]  []
1   []  [a]  []
2  [d]  [d]  []

df1 = df.loc[:, df.astype(bool).all()]
print (df1)
     b
0  [d]
1  [a]
2  [d]
print (df.astype(bool))
       a     b      c
0  False  True  False
1  False  True  False
2   True  True  False