user341903
user341903

Reputation: 1

How to replace in pandas dataframe with for loop from lists?

I'm trying to replace a single column values row by row (for now settling with overall dataframe replace) based off of a list of words to replace if words are found in a different list. They have matched length so indexing should work.

Ex. If "friend" in list one, replace with "buddy" from list two.

data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
pd.DataFrame.from_dict(data)

numlist = [1,2,3]
abclist = ["z","x","y"]

for n in numlist:
pd.DataFrame.replace(n, abclist[numlist.index(n)])

Getting error: Please Help

TypeError                                 Traceback (most recent call 
last)
<ipython-input-12-4e44f23fd530> in <module>()
  6 
  7 for n in numlist:
----> 8     pd.DataFrame.replace(n,abclist[numlist.index(n)])
  9 DataFrame

/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in 
replace(self, to_replace, value, inplace, limit, regex, method)
3790     def replace(self, to_replace=None, value=None, inplace=False, 
limit=None,
3791                 regex=False, method='pad'):
->  3792         return super(DataFrame, 
self).replace(to_replace=to_replace,
3793                                               value=value, 
inplace=inplace,
3794                                               limit=limit,   
regex=regex,

TypeError: super(type, obj): obj must be an instance or subtype of type

Upvotes: 0

Views: 4314

Answers (1)

sacuL
sacuL

Reputation: 51335

Generally, when working with dataframes, iteration is not the best method. You can use pandas methods instead:

In your case, you can create a dictionary for your replacements using zip, and then use replace.

Starting Dataframe:

>>> df
   col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d

For whole dataframe replacement:

my_dict = dict(zip(numlist, abclist))

df.replace(my_dict, inplace=True)

>>> df
  col_1 col_2
0     y     a
1     x     b
2     z     c
3     0     d

Or for single column replacements (here, replacing only in col_1):

my_dict = dict(zip(numlist, abclist))

df['col_1'].replace(my_dict, inplace=True)

>>> df
  col_1 col_2
0     y     a
1     x     b
2     z     c
3     0     d

Upvotes: 1

Related Questions