I have a dataframe like this:
|name |code|
|---------|----|
|fa.st.pdf|10 |
|creed.txt|10 |
|logs.log |11 |
|log.s.txt|11 |
|foo.bar |11 |
what I need is to create a progressive filename for each code, like this:
|name |code|
|-----|----|
|1.pdf|10 |
|2.txt|10 |
|1.log|11 |
|2.txt|11 |
|3.bar|11 |
Thanks in advance!!
CodePudding user response:
Use groupby.cumcount
to enumerate the rows per group and str.extract
to get the file extension. Concatenate both as string:
df['name'] = (df.groupby('code').cumcount().add(1).astype(str)
df['name'].str.extract('(\.[^.] $)', expand=False)
)
Output:
name code
0 1.pdf 10
1 2.txt 10
2 1.log 11
3 2.txt 11
4 3.bar 11