Skip to main content
 首页 » 编程设计

python之如何从数据框中每一行的列表中减少一些值

2025年05月04日19shihaiming

我希望按某个值从列表的每一行的每一列中删除一些值。

b = pd.DataFrame({"a":[1,1,1,2,2], 
                 "b":[6,7,8,9,10]}) 
 
b.groupby("a")["b"].apply(list).apply() # I try to group it but I do not know how to finish it after that. Any idea how to continue from this code?  

我的想法是将所有“a”分组,然后从每组中切出一个数据集或一行。

    a   b 
0   1   6 
1   1   7 
2   1   8 
3   2   9 
4   2   10 
 
 
 
    a   b 
0   1   6 
1   1   7 
3   2   9 

请您参考如下方法:

所以重复

b[b.iloc[::-1].duplicated('a')] 
   a  b 
0  1  6 
1  1  7 
3  2  9