'Reduce rows of a matrix with certain columns unequal to 0
I have a pandas dataframe with n columns and m rows. All entries are either 0,1 or -1. The rows are linearly independent of each other. Furthermore, I have a real subset G of the columns. I want a function that reduces the number of rows where at least one of the entries in the columns G is not equal to 0. However, each column must contain at least one element not equal to 0. Can anyone help me? Thank you in advance.
Example 1: G = ["c2"]
pd.DataFrame(data=np.array([[1,0,0,0],[0,1,1,1],[0,1,1,-1],[0,1,0,0]]),columns=["c1","c2","c3","c4"],index=["r1","r2","r3","r4"])
c1 c2 c3 c4
r1 1 0 0 0
r2 0 1 1 1
r3 0 1 1 -1
r4 0 1 0 0
I would like to eliminate G = ["c2"] from most of the rows (means they shall have the entry = 0), with the constraint that it has to occur in minimum one row. In that case I would calculate r2 = r2-r4 and r3=r3-r4.
Example 2: G=["c3","c4"]
pd.DataFrame(data=np.array([[1,1,1,1],[0,1,1,1],[0,0,1,1],[0,0,0,1]]),columns=["c1","c2","c3","c4"],index=["r1","r2","r3","r4"]
c1 c2 c3 c4
r1 1 1 1 1
r2 0 1 1 1
r3 0 0 1 1
r4 0 0 0 1
Here I would calculate r2 = r2-r3 and r1=r1-r3. It would be more favourable if I could remove c4 also from r4,so I would have only one row (r3) with a c3 or c4 != 0, but this is not possible without having c3 != 0.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
