python - pandas DataFrame filter by rows and columns -


i have python pandas dataframe looks this:

                        b      c    ...     zz 2008-01-01 00    nan    nan    nan    ...      1 2008-01-02 00    nan    nan    nan    ...    nan 2008-01-03 00    nan    nan      1    ...    nan ...              ...    ...    ...    ...    ... 2012-12-31 00    nan      1    nan    ...    nan 

and can't figure out how subset of dataframe there 1 or more '1' in it, final df should this:

                   b      c    ...     zz 2008-01-01 00    nan    nan    ...      1 2008-01-03 00    nan      1    ...    nan ...              ...    ...    ...    ... 2012-12-31 00    1      nan    ...    nan 

this is, removing rows , columns not have 1 in it.

i try seems remove rows no 1:

df_filtered = df[df.sum(1)>0] 

and try remove columns with:

df_filtered = df_filtered[df.sum(0)>0] 

but error after second line:

indexingerror('unalignable boolean series key provided') 

do loc:

in [90]: df out[90]:     0   1   2   3   4   5 0   1 nan nan   1   1 nan 1 nan nan nan nan nan nan 2   1   1 nan nan   1 nan 3   1 nan   1   1 nan nan 4 nan nan nan nan nan nan  in [91]: df.loc[df.sum(1) > 0, df.sum(0) > 0] out[91]:    0   1   2   3   4 0  1 nan nan   1   1 2  1   1 nan nan   1 3  1 nan   1   1 nan 

here's why error:

let's have following frame, df, (similar yours):

in [112]: df out[112]:       b   c   d   e 0   0   1   1 nan   1 1 nan nan nan nan nan 2   0   0   0 nan   0 3   0   0   1 nan   1 4   1   1   1 nan   1 5   0   0   0 nan   0 6   1   0   1 nan   0 

when sum along rows , threshold @ 0, get:

in [113]: row_sum = df.sum()  in [114]: row_sum > 0 out[114]:     true b     true c     true d    false e     true dtype: bool 

since index of row_sum columns of df, doesn't make sense in case try use values of row_sum > 0 fancy-index rows of df, since row indices not aligned , cannot aligned.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

debian - 500 Error upon login into Plesk Admin - auth.php3? -