試試這個嗎?
數據輸入:
df=pd.DataFrame({'name':['A','B','C'],'foo_list':[[('bleh'),('blah')], [(), 'boo'],[(),(),()]]})
解決方案:
df['foo_list']=df['foo_list'].apply(lambda x : [t for t in x if t !=()])
df.loc[df['foo_list'].apply(len)>0,:]
Out[20]:
foo_list name
0 [bleh, blah] A
1 [boo] B
時序(小尺寸)
%timeit df['foo_list'].apply(lambda x : [t for t in x if t !=()])#Wen
10000 loops, best of 3: 117 µs per loop
%timeit df.foo_list.apply(lambda x: filter(None, x)) # John
10000 loops, best of 3: 121 µs per loop
大尺寸會推薦約翰的解決方案
df=pd.concat([df]*10000,0)
%timeit df.foo_list.apply(lambda x: filter(None, x))
100 loops, best of 3: 10.2 ms per loop
%timeit df['foo_list'].apply(lambda x : [t for t in x if t !=()])
100 loops, best of 3: 17.1 ms per loop
來源
2017-09-17 05:10:03
Wen
我得到了相同的答案,但我用''DF – Dark
@Bharathshetty啊哈[DF [ 'foo_list']申請(LEN)> 0。]: ) – Wen
@JohnGalt完全不需要,你的解決方案已經比我的速度快:) – Wen