Python 如何删除在多索引数据框中只有一个条目的行?
我有以下类型的多索引数据帧:Python 如何删除在多索引数据框中只有一个条目的行?,python,pandas,pandas-groupby,multi-index,Python,Pandas,Pandas Groupby,Multi Index,我有以下类型的多索引数据帧: import random col3=[0,0,0,0,2,4,6,0,0,0,100,200,300,400] col4=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500] d = {'Unit': [1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6], 'Year': [2014, 2015, 2016, 2017, 2015, 2016, 2017, 2017, 2014, 2015, 2014
import random
col3=[0,0,0,0,2,4,6,0,0,0,100,200,300,400]
col4=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500]
d = {'Unit': [1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6],
'Year': [2014, 2015, 2016, 2017, 2015, 2016, 2017, 2017, 2014, 2015, 2014, 2015, 2016, 2017], 'col3' : col3, 'col4' : col4 }
df = pd.DataFrame(data=d)
new_df = df.groupby(['Unit', 'Year']).sum()
col3 col4
Unit Year
1 2014 0 0
2015 0 0
2016 0 0
2017 0 0
2 2015 2 4
2016 4 6
2017 6 8
3 2017 0 0
4 2014 0 0
5 2015 0 0
6 2014 100 200
2015 200 900
2016 300 400
2017 400 500
事实上,它当然更大,但这确实起到了作用。在这个数据框架中,我想删除所有只有一年条目的单元。所以我想要这个:
col3 col4
Unit Year
1 2014 0 0
2015 0 0
2016 0 0
2017 0 0
2 2015 2 4
2016 4 6
2017 6 8
6 2014 100 200
2015 200 900
2016 300 400
2017 400 500
提前感谢您的帮助
Jen与任何列和测试计数一起使用,比较不等于,过滤方式:
或获取索引依据和筛选依据的值:
谢谢你,那确实是个错误。也应该删除,我编辑!
df = new_df[new_df.groupby(level=0)['col3'].transform('size').ne(1)]
df = new_df[new_df.index.get_level_values(0).duplicated(keep=False)]
print (df)
col3 col4
Unit Year
1 2014 0 0
2015 0 0
2016 0 0
2017 0 0
2 2015 2 4
2016 4 6
2017 6 8
6 2014 100 200
2015 200 900
2016 300 400
2017 400 500