I want to replace part of a column value in a dataframe using values from other columns

baris_a

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.

ifly6

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.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas Dataframe replace part of string with value from another column

data.table in R: Replace a column value with a value from same column after matching two other columns values

Replace values in column with column values from other dataframe in Python

Setting value of a column based on values of other columns in Pandas dataframe

Python: I want to check if row has multiple same values for any of the columns in a dataframe and if yes, replace the repeated value with null

Pandas Dataframe - Group by column value and lookup values from other columns

Creating a 2D list from a column using values from 2 other columns in the same dataframe

How to replace string values in one column with actual column values from other columns in the same dataframe?

I want to sum the rows of a column in multiindex-dataframe but keep the values of other columns

Replace column values using key value dataframe

I want to create a column in a dataframe using a dict value from the dataframe

Replace NaN values in one column from two other columns

How do I replace a string from one column using the data from two other columns (pandas)

I have dictionary as value in pandas dataframe columns. I want to make the keys columns and values as column value

Create new column into dataframe based on values from other columns using apply function onto multiple columns

Replace NaN values in pandas dataframe with a computation from other columns

Replace empty values with value from other column in a dataframe

Replace the value in column with respect to other two columns values

Replace values in column based on values in two other columns using pandas

I have 2 pandas dataframes and want to pull values from one dataframe to fill in columns of the other?

replace dataframe column values with values from an other dataframe column

Replace values from non-identical columns in other dataframe

Replacing value of a column conditioned on values of other columns in the DataFrame

Replace values in dataframe based on other dataframe with column name and value

I want to populate the column of a dataframe with values from the column of another dataframe when the values of two columns match

How can I query a column of a dataframe on a specific value and get the values of two other columns corresponding to that value

How do I add a column to a dataframe based on values from other columns?

How to replace a part of column value with values from another two columns based on a condition in pandas

How can I subtract values of each column of my dataframe from all other columns?