熊猫:如果包含多于2个条目,如何删除行?

我有一个如下的数据框

df
        entry
0       (5, 4)
1       (4, 2, 1)
2       (0, 1)
3       (2, 7)
4       (9, 4, 3)

I would like to keep only the entry that contains two values

df
     entry
0    (5, 4)
1    (0, 1)
2    (1, 7)
评论
uid
uid

If there are tuples use Series.str.len for lengths and compare by Series.le for <= and filter in boolean indexing:

df1 = df[df['entry'].str.len().le(2)]
print (df1)
    entry
0  (5, 4)
2  (0, 1)
3  (2, 7)

If there are strings compare number of , and compare by Series.lt for <:

df2 = df[df['entry'].str.count(',').lt(2)]
print (df2)
   entry
0  (5,4)
2  (0,1)
3  (2,7)
点赞
评论