pandas - Python - Remove rows and columns in Matrix where all values are 0 -
i have matrix created dataframe , want remove columns every value in 0.
i have seen examples using dropna df2.loc[:, (df2 != 0).any(axis=0)]
doesnt dataframe.
this how created matrix:
a = ['psychology','education','social policy','sociology','pol. sci. & internat. studies','development studies','social anthropology','area studies','science , technology studies','law & legal studies','economics','management & business studies','human geography','environmental planning','demography','social work','tools, technologies & methods','linguistics','history'] final_df = new_df[new_df['subject'].isin(a)] ctrs = {location: counter(gp.grantrefnumber) location, gp in final_df.groupby('subject')} ctrs = list(ctrs.items()) overlaps = [(loc1, loc2, sum(min(ctr1[k], ctr2[k]) k in ctr1)) i, (loc1, ctr1) in enumerate(ctrs, start=1) (loc2, ctr2) in ctrs[i:] if loc1 != loc2] overlaps += [(l2, l1, c) l1, l2, c in overlaps] df22 = pd.dataframe(overlaps, columns=['loc1', 'loc2', 'count']) df22 = df22.set_index(['loc1', 'loc2']) df22 = df22.unstack().fillna(0).astype(int) #the end part of next line filters top 'x' amount. b = np.sort(np.unique(df22.values.ravel()))[-20:] df2 = df22.where(df22.isin(b),0.0)
interestingly (or not), when type df2.columns
, get:
multiindex(levels=[[u'count'], [u'area studies', u'demography', u'development studies', u'economics', u'education', u'environmental planning', u'history', u'human geography', u'law & legal studies', u'linguistics', u'management & business studies', u'pol. sci. & internat. studies', u'psychology', u'science , technology studies', u'social anthropology', u'social policy', u'social work', u'sociology', u'tools, technologies & methods']], labels=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]], names=[none, u'loc2'])
which might why struggling.
you need all
true
of columns contains 0
~
invert condition:
df = pd.dataframe({'b':[0,0,0,0,0,0], 'c':[0,8,9,4,2,3], 'd':[0,3,5,7,1,0], 'e':[0,3,6,9,2,4]}) print (df) b c d e 0 0 0 0 0 1 0 8 3 3 2 0 9 5 6 3 0 4 7 9 4 0 2 1 2 5 0 3 0 4 df = df.loc[~df.eq(0).all(axis=1), ~df.eq(0).all()] print (df) c d e 1 8 3 3 2 9 5 6 3 4 7 9 4 2 1 2 5 3 0 4
Comments
Post a Comment