I have a column where part of the column is to be replaced by a value from another column. For example I want to move from this:
<table style="width:100%" border="1">
<tr>
<th>Reference</th>
<th>Identification\Customer</th>
<th>Target Customer</th>
</tr>
<tr>
<td>CustomerA\BFG\CustomerA-CCP\CustomerA-CSA</td>
<td>CustomerA</td>
<td>CustomerB</td>
</tr>
</table>
to this
<table style="width:100%" border="1">
<tr>
<th>Reference
</th>
<th>Identification\Customer</th>
<th>Target Customer</th>
</tr>
<tr>
<td>Customer<a style="color:red;">B</a>\BFG\CustomerA-CCP\CustomerA-CSA</td>
<td>CustomerA</td>
<td>CustomerB</td>
</tr>
</table>
I only want the initial value of CustomerA to be changed to CustomerB. The rest of the values should stay the same.
I thought this should work but I get all na in the column
data = [['CustomerA\\BFG\\CustomerA-CCP\\CustomerA-Agreement', 'CustomerA', 'CustomerB'],['CustomerC\\BFG\\CustomerC-CCP\\CustomerC-Agreement', 'CustomerC', 'CustomerD']]
customerCollateral = pd.DataFrame(data, columns = ['Reference', 'Identification\\Customer','Identification\\Parent'])
customerCollateral['Reference2']=customerCollateral.apply(lambda x:x['Reference'].replace(x['Identification\\Customer'],x['Identification\\Parent'],n=1),axis=1)
print(customerCollateral)
however when I run the above I got this error. TypeError: ('replace() takes no keyword arguments', 'occurred at index 0')
If I do not use n=1 ten all the values of CustomerA is replaced with CustomerB.
x['Reference']
is the object in question, since x
is a Series and it's already queried by name. The object that is being called with replace(...)
is str
.
But, str.replace
does not have a keyword n
. See the docs here, the limit is defined by a third parameter, not a keyword parameter: https://docs.python.org/3/library/stdtypes.html#str.replace
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments