Pandas convert df to matrix by condition

Is it possible to hide df in the matrix as follows? Given df:

Name Value
x    5
x    2
x    3
x    3
y    3
y    2
z    4

The matrix will be:

Name    1    2    3   4   5   
x       4    4    3   1   1
y       2    2    1   0   0
z       1    1    1   1   0

Here's the logic:

Name    1    2    3  4    5   (5 columns since 5 is the max in Value)
--------------------------------------------------------------------
x       4 (since x has 4 values >= 1)     4 (since x has 4 values >= 2)    3 (since x has 3 values >= 3)   1 (since x has 1 values >= 4)   1 (since 1 x >= 5)
y       2 (since y has 2 values >= 1)     2 (since y has 2 values >= 2)    1 (since y has 1 values >= 3)   0 (since no more y >= 5)        0 (since no more y >= 5)
z       1 (since z has 1 values >= 1)     1 (since z has 1 values >= 2)    1 (since z has 1 values >= 3)   1 (since z has 1 values >= 4)   0 (since no more z >= 5)

Let me know if that makes sense.
I know that I have to use sorting, group and count, but could not figure out how to set up the matrix.

Thank!!!

+4
source share
5 answers

Probably the fastest solution using broadcast numpyis

i = np.arange(1, df.Value.max() + 1)
j = df.Value.values[:, None] >= i

df = pd.DataFrame(j, columns=i, index=df.Name).sum(level=0)

        1    2    3    4    5
Name                         
x     4.0  4.0  3.0  1.0  1.0
y     2.0  2.0  1.0  0.0  0.0
z     1.0  1.0  1.0  1.0  0.0

Warning. In exchange for performance, this is a somewhat hungry method. For big data, this can lead to memory exiting, so use it with discretion.


More details

Create a range of values ​​from 1to df.Value.max()-

i = np.arange(1, df.Value.max() + 1)
i
array([1, 2, 3, 4, 5])

df.Values i -

j = df.Value.values[:, None] >= i
j

array([[ True,  True,  True,  True,  True],
       [ True,  True, False, False, False],
       [ True,  True,  True, False, False],
       [ True,  True,  True, False, False],
       [ True,  True,  True, False, False],
       [ True,  True, False, False, False],
       [ True,  True,  True,  True, False]], dtype=bool)

df.Name, .

k = pd.DataFrame(j, columns=i, index=df.Name)
k
         1     2      3      4      5
Name                                 
x     True  True   True   True   True
x     True  True  False  False  False
x     True  True   True  False  False
x     True  True   True  False  False
y     True  True   True  False  False
y     True  True  False  False  False
z     True  True   True   True  False
k.sum(level=0)

        1    2    3    4    5
Name                         
x     4.0  4.0  3.0  1.0  1.0
y     2.0  2.0  1.0  0.0  0.0
z     1.0  1.0  1.0  1.0  0.0

, .astype(int) -

k.sum(level=0).astype(int)

      1  2  3  4  5
Name               
x     4  4  3  1  1
y     2  2  1  0  0
z     1  1  1  1  0
+8

, :

d2 = df.pivot_table(index="Name", columns="Value", aggfunc=len)
d2 = d2.reindex(range(1, df["Value"].max()+1), axis=1).fillna(0)
d2 = d2.iloc[:, ::-1].cumsum(axis=1).iloc[:, ::-1]

In [115]: d2
Out[115]: 
Value    1    2    3    4    5
Name                          
x      4.0  4.0  3.0  1.0  1.0
y      2.0  2.0  1.0  0.0  0.0
z      1.0  1.0  1.0  1.0  0.0

.iloc[:, ::-1] - , .

+4

, , -

import pandas as pd
import numpy as np

df = pd.DataFrame({"Name":["x","x","x","x","y","y","z"],
                  "Value":[5,2,3,3,3,2,4]})

mv = df["Value"].max()
out=[]
for i in range(mv):
    out.append(df.groupby("Name").apply(lambda x : len(x[x["Value"]>=i+1])))

df2  = pd.concat(out, axis=1)
df2.columns = np.arange(1,mv+1)
+3

groupby:

def get_counts(frame, idx):
    idx = np.arange(1, idx+1)[::-1]
    vc = frame['Value'].value_counts().reindex(idx)
    return vc.cumsum().ffill().sort_index().fillna(0.).astype(int)

idx = df['Value'].max()
print(df.groupby('Name').apply(lambda f: get_counts(f, idx)))

Value  1  2  3  4  5
Name                
x      4  4  3  1  1
y      2  2  1  0  0
z      1  1  1  1  0

, " ", groupby.

+2

, pd.cut, , float: -)

df['G']=pd.cut(df.Value,list(range(df.Value.max()+1)),labels=list(range(1,df.Value.max()+1)))

df1=df.groupby(['Name','G']).count().sort_index(level='G',ascending=False).\
         groupby(level='Name').cumsum().\
             Value.unstack().bfill(1).fillna(0)
df1
Out[398]: 
G       1    2    3    4    5
Name                         
x     4.0  4.0  3.0  1.0  1.0
y     2.0  2.0  1.0  0.0  0.0
z     1.0  1.0  1.0  1.0  0.0
+2

Source: https://habr.com/ru/post/1691077/


All Articles