Reputation: 790
I have a pandas data frame
list1 = ['A','B', 'C', 'D' ]
list2 = ['P','Q', 'R', 'S' ]
list3 = ['A', 'B', 'C', 'D']
list4 = [[1,3,5], [], [2,4,1,5], []]
df = pd.DataFrame({'col1' : list1,
'col2' : list2,
'col3':list3,
'numlist' : list4
})
print(df)
col1 col2 col3 numlist
0 A P A [1, 3, 5]
1 B Q B []
2 C R C [2, 4, 1, 5]
3 D S D []
I want to get the index of all rows where the column numlist is not empty and then create a new data frame with rows matching the indexes. I am trying the following code
df[(len(df['numlist']))==0]
But this code is throwing Key Error. How can I achieve the same?
Upvotes: 0
Views: 136
Reputation: 7693
You can use apply
and check with x != []
condition that list is empty or not.
>>> df = df[df.numlist.apply(lambda x: x != [])]
>>> df
col1 col2 col3 numlist
0 A P A [1, 3, 5]
2 C R C [2, 4, 1, 5]
Upvotes: 1
Reputation: 862641
Empty lists converted to boolen return False
is possible filtering this way:
df1 = df[df['numlist'].astype(bool)]
Your solution should be changed by Series.str.len
with Series.ne
for not equal:
df1 = df[df['numlist'].str.len().ne(0)]
Alternative:
df1 = df[df['numlist'].str.len() != 0]
print(df1)
col1 col2 col3 numlist
0 A P A [1, 3, 5]
2 C R C [2, 4, 1, 5]
Upvotes: 3