teller.py3
teller.py3

Reputation: 844

Subsetting dataframe when comparing two columns with pandas

Let say I have a dataframe called df:

    name        X         Y
0   John        1         1
1   Cindy       1         0
2   Ella        0         1
3   David       0         0

I would like to compare columns X and Y such that when they are both 1, the corresponding row is added to a new data frame called df2.

The desired output of df2 would be this:

    name        X         Y
0   John        1         1

Moreover, I would like to do the same when

Upvotes: 0

Views: 52

Answers (2)

SomeDude
SomeDude

Reputation: 14238

Try :

dfs = list(dict(list(df.groupby(['X','Y']))).values())

Upvotes: 0

rhug123
rhug123

Reputation: 8768

Try this:

dfs = [y for x,y in df.groupby(['X','Y'])]

df2 = dfs[0]
df3 = dfs[1]
df4 = dfs[2]
df5 = dfs[3]

Upvotes: 2

Related Questions