Federico Trotta
Federico Trotta

Reputation: 15

rename all the values in a Pandas column using an iteration (a for loop)

I have the need to rename all the values in a Pandas column (from a data frame) and I want the new values to have "iterated-like" new names.

Let's say my data frame (df) has a column named "animals" with some values like "dog, cat, pig, etc", which are all strings. I want to rename all the values and I want the new values to be like "animal_1, animal_2, animal_3, etc".

I've tried something like:

for i in range (1, 20):
   for val in df['animals']:
      df['animals'].replace({'val' : 'animal_(i)'})

but it does not work at all...

[EDIT]

What is important is that for each "old value" the "new value" has to be the same. This means that, i.e., if I have 32 values which are "dog"...any "dog" value becomes the same "animal_x", where I'm not interested in the "x" value that "i" has become...

This is why I've tried to use a for loop, with no good solution...

Any suggestions on how to do it?

Upvotes: 0

Views: 558

Answers (1)

jezrael
jezrael

Reputation: 862511

If there is default RangeIndex is possible use:

df['animals'] = 'animal_' + (df.index + 1).astype(str)

Or use list comprehension with f-strings:

df['animals'] = [f'animal_{i + 1}' for i in df.index]

For any index:

df['animals'] = [f'animal_{i + 1}' for i in np.arange(len(df.index))]

EDIT: If need same number for each type of animal use:

df['new'] = [f'animal_{i + 1}' for i in pd.factorize(df['animals'])[0]]
print (df)
  animals       new
0     dog  animal_1
1     cat  animal_2
2     pig  animal_3
3     dog  animal_1
4     dog  animal_1
5     pig  animal_3

Upvotes: 2

Related Questions