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
for num,abc in zip(numlist,abclist): ...replace(num,abc). Other than that, the code is correct. What is your question?