Input:
ColumnA:
A
A
B
B
C
C
Output
ColumnB:
0
1
0
1
0
1
The condition is: The column B will be 0 if the value in column A is the first time appear. Otherwise the column B will be 1. Thanks! Using pandas in Python.
Input:
ColumnA:
A
A
B
B
C
C
Output
ColumnB:
0
1
0
1
0
1
The condition is: The column B will be 0 if the value in column A is the first time appear. Otherwise the column B will be 1. Thanks! Using pandas in Python.
Use duplicated + astype mask to int:
print (df.duplicated())
0 False
1 True
2 True
3 False
4 True
5 False
6 True
dtype: bool
df['ColumnB'] = df.duplicated().astype(int)
print (df)
ColumnA ColumnB
0 A 0
1 A 1
2 A 1
3 B 0
4 B 1
5 C 0
6 C 1
df=pd.DataFrame({'ColumnA': {0: 'A', 1: 'A', 2: 'B', 3: 'B', 4: 'C', 5: 'C'}})
df
Out[284]:
ColumnA
0 A
1 A
2 B
3 B
4 C
5 C
Use an apply to check if the value has appeared before.
df['ColumnB'] = df.apply(lambda x: int(x.ColumnA in df.iloc[:x.name,0].tolist()), axis=1)
df
Out[287]:
ColumnA ColumnB
0 A 0
1 A 1
2 B 0
3 B 1
4 C 0
5 C 1