Reputation: 719
Lets say I have this pandas dataframe:
index a b
1 'pika' 'dog'
2 'halo' 'cat'
3 'polo' 'dog'
4 'boat' 'man'
5 'moan' 'tan'
6 'nope' 'dog'
and I have a list like this:
colors = ['black' , 'green', 'yellow']
How would I replace all the dog
in column b
with the elements
in the colors
list in the same order?
Basically, I want it to look something like this:
index a b
1 'pika' 'black'
2 'halo' 'cat'
3 'polo' 'green'
4 'boat' 'man'
5 'moan' 'tan'
6 'nope' 'yellow'
Upvotes: 0
Views: 551
Reputation: 323226
You can check with
n=(df.b=="'dog'").sum()
df.loc[df.b=="'dog'",'b']=(['black' , 'green', 'yellow']*(n//3))[:n]
Upvotes: 0
Reputation: 914
Another way using numpy put
import pandas as pd
import numpy as np
df = pd.DataFrame({'a': ['pika', 'halo', 'polo', 'boat', 'moan', 'nope'],
'b': ['dog', 'cat', 'dog', 'man', 'tan', 'dog']})
colors = ['black' , 'green', 'yellow']
df
a b
0 pika dog
1 halo cat
2 polo dog
3 boat man
4 moan tan
5 nope dog
-
# 'wrap' mode is not needed when replacement list is same
# size as the number of target values
np.put(df.b, np.where(df.b == 'dog')[0], colors, mode='wrap')
df
a b
0 pika black
1 halo cat
2 polo green
3 boat man
4 moan tan
5 nope yellow
Upvotes: 0
Reputation: 3801
Use itertools.cycle
, df.apply
, and lambda
In [100]: import itertools as it
In [101]: colors_gen = it.cycle(colors)
In [102]: df1['c'] = df1['b'].apply(lambda x: next(colors_gen) if x == 'dog' else x)
In [103]: df1
Out[103]:
a b c
0 pika dog black
1 halo cat cat
2 polo dog green
3 boat man man
4 moan tan tan
5 nope dog yellow
This will also work for larger DataFrames
In [104]: df2 = pd.DataFrame({'a': ['pika', 'halo', 'polo', 'boat','moan','nope','etc','etc'], 'b':['dog','cat','dog','man','tan','dog','dog','dog']})
In [106]: df2['c'] = df2['b'].apply(lambda x: next(colors_gen) if x == 'dog' else x)
In [107]: df2
Out[107]:
a b c
0 pika dog black
1 halo cat cat
2 polo dog green
3 boat man man
4 moan tan tan
5 nope dog yellow
6 etc dog black
7 etc dog green
Upvotes: 0
Reputation: 164663
Using pd.DataFrame.loc
and Boolean indexing:
df.loc[df['b'].eq('dog'), 'b'] = colors
print(df)
index a b
0 1 pika black
1 2 halo cat
2 3 polo green
3 4 boat man
4 5 moan tan
5 6 nope yellow
Upvotes: 3