user16386186
user16386186

Reputation:

How can I create a new column containing 0 and 1 values via groupby("col1")?

I have a dataframe like this:

df = pd.DataFrame({"col1":["a","a","a","b","b","c","c","c","c","d"]})

How can I create a new column containing 0 and 1 values via groupby("col1") ?

  col1 col2
0   a   0
1   a   0
2   a   0
3   b   1
4   b   1
5   c   0
6   c   0
7   c   0
8   c   0
9   d   1

Upvotes: 0

Views: 93

Answers (3)

Brendan
Brendan

Reputation: 4011

[It appears the question was asking about flagging every other group with 0/1; this was not clear from the initial framing of the question, so this answer perhaps appears overly simplistic.]

Check if col1 is either b or d and convert the boolean True/False to an integer:

df = pd.DataFrame({"col1":["a","a","a","b","b","c","c","c","c","d"]})

df['col2'] = df['col1'].isin(['b','d']).astype(int)
  col1  col2
0    a     0
1    a     0
2    a     0
3    b     1
4    b     1
5    c     0
6    c     0
7    c     0
8    c     0
9    d     1

Upvotes: 0

mozway
mozway

Reputation: 261860

You can groupby col1 and take the remainder of the group number divided by 2:

df['col2'] = df.groupby('col1', sort=False).ngroup()%2

output:

  col1  col2
0    a     0
1    a     0
2    a     0
3    b     1
4    b     1
5    c     0
6    c     0
7    c     0
8    c     0
9    d     1

Alternative form:

df['col2'] = df.groupby('col1', sort=False).ngroup().mod(2)

And in case you want odd groups to be 1 and even groups 0:

df['col2'] = df.groupby('col1', sort=False).ngroup().add(1).mod(2)

Upvotes: 3

BENY
BENY

Reputation: 323316

Without groupby try factorize

df['new'] = df.col1.factorize()[0]%2
df
Out[151]: 
  col1  new
0    a    0
1    a    0
2    a    0
3    b    1
4    b    1
5    c    0
6    c    0
7    c    0
8    c    0
9    d    1

Or try with

from itertools import cycle
df['new'] = df.col1.map(dict(zip(df.col1.unique(), cycle([0,1]))))
df
Out[155]: 
  col1  new
0    a    0
1    a    0
2    a    0
3    b    1
4    b    1
5    c    0
6    c    0
7    c    0
8    c    0
9    d    1

Upvotes: 3

Related Questions