Reputation: 251
Here is the dataframe which I have:
import pandas as pd
list = ['RK','G', 'CUSTOM', 'CUSTOM', 'KL', 'kj']
df=pd.DataFrame(list,columns=['A'])
What I would like to do here is to find out all the entries in column A
which are equal to CUSTOM
and replace it with CUSTOM1
AND CUSTOM2
.
The output should be:
A
0 RK
1 G
2 CUSTOM1
3 CUSTOM2
4 KL
5 kj
Had there been 3 CUSTOM
instead of 2, the output should be CUSTOM1
, CUSTOM2
and CUSTOM3
.
Thanks.
Upvotes: 2
Views: 153
Reputation: 96286
I'm interested to see how this approach will work compared to the others:
In [10]: import itertools
In [11]: counter = itertools.count(1)
In [12]: df.A.str.replace(r"CUSTOM", lambda x: f"CUSTOM{next(counter)}")
Out[12]:
0 RK
1 G
2 CUSTOM1
3 CUSTOM2
4 KL
5 kj
Name: A, dtype: object
Note, you need pandas
version >= 0.20.0 for pd.Series.str.replace
to accept a callable.
If you don't have Python 3.6 f-strings, you can also just do:
In [14]: counter = itertools.count(1)
In [15]: df.A.str.replace(r"CUSTOM", lambda x: "CUSTOM{}".format(next(counter)))
Out[15]:
0 RK
1 G
2 CUSTOM1
3 CUSTOM2
4 KL
5 kj
Name: A, dtype: object
Upvotes: 2
Reputation: 403128
Using cumsum
+ np.where
-
m = df.A.eq('CUSTOM')
df.A = np.where(m, df.A + m.cumsum().astype(str), df.A)
df
A
0 RK
1 G
2 CUSTOM1
3 CUSTOM2
4 KL
5 kj
A similar solution using pd.Series.where
/mask
-
df.A = df.A.where(~m, df.A + m.cumsum().astype(str))
Or,
df.A = df.A.mask(m, df.A + m.cumsum().astype(str))
df
A
0 RK
1 G
2 CUSTOM1
3 CUSTOM2
4 KL
5 kj
Upvotes: 2
Reputation: 4345
You could do it with a loop:
count = 1
for index, row in df.iterrows():
if row['A'] == 'CUSTOM':
row['A'] += str(count)
count += 1
print(df)
#output
A
0 RK
1 G
2 CUSTOM1
3 CUSTOM2
4 KL
5 kj
Upvotes: 1
Reputation: 323376
By using cumcount
+ count
df.A=df.A+(df.groupby('A').cumcount()+1).where(df.groupby('A').A.transform('count').ne(1),'').astype(str)
df.A
Out[208]:
0 RK
1 G
2 CUSTOM1
3 CUSTOM2
4 KL
5 kj
dtype: object
Upvotes: 2