Home > Back-end >  Replace string from column in Python dataframe
Replace string from column in Python dataframe

Time:08-24

In reference to: python replace string in a specific dataframe column

I'm trying search a column, replace a string and remove a string. Can anyone help?

Example: 0369BODMIN
Replace initial '0' with 'GIF'
Delete anything after the 5th character ([5:])
So it becomes GIF369

Using pandas data-frame and regex.

df.loc[df['df'].str.lower().str.startswith('0'), 'CustomerID'] = 
df['CustomerID'].str.extract('(Chaud\w  )', expand=False)

excel sheet example

Edit: My code - modified to loop through the Customer Code column, but when I export the dataframe it only exports the Customer Code column instead of the whole DF.

import pandas as pd
customer_data = pd.read_csv(r'C://customers.csv')
customer_data = customer_data['*Customer Code'].apply(lambda x: x.replace('0','GIF',1)[:6])
print(customer_data)
customer_data.to_csv('customersMerged.csv', index=False)

CodePudding user response:

Does it need to be with regex patterns? You could also use the apply and lambda functions, as below:

The first two lines I'm just generating a df with column name 'Customer Code' and value '0765POOLE'.

df = pd.DataFrame(columns=['Customer Code'])
df.loc[0,'Customer Code'] = '0765POOLE'
df['Customer Code'] = df['Customer Code'].apply(lambda x: x.replace('0','GIF',1)[:6])
print(df)

Output:
  Customer Code
0        GIF765

PLEASE NOTE:

  1. This code will replace the first occurrence of a 0 with GIF in all values of the 'Customer Code' column and then slice it to keep only the first 6 characters.
  • Related