在我的数据框中,有几行的值类似于“ []”,我想删除整列,如何完成它?
Data Frame Image 这是我的代码
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
答案 0 :(得分:4)
我相信您需要强制转换值以使用DateTime2
布尔值和仅过滤True
的列:
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