I have a sample data frame created like this
import pandas as pd
data = {'Client Name' : ['ABC CO PVT', 'ABC CO PVT', 'ONE PRIVATE', 'Good & Bad Limited']}
df = pd.DataFrame(data, columns = ['Client Name'] )
I need to add an empty column to the dataframe called Transformation applied.
I want to transform it in such a way that if the Client Name has PVT , a new row is added to the DataFrame by replacing PVT with PRIVATE and the corresponding new column "Transformation applied should be populated as PRIVATE. If there is a '&', I want to add a new row by replacing & with text AND the corresponding new column "Transformation applied should be populated as AND.
The output dataframe should be something like this:
Client Name | Transformation applied |
---|---|
ABC CO PVT | NONE |
ABC CO PVT | PRIVATE |
ONE PRIVATE | NONE |
Good & Bad Limited | Good AND Bad Limited |
A new row is inserted with transformed text.
I've tried creating functions to transform the data.
def Private (text):
text = str(text)
if "PVT" in text:
M_text = text.replace('PVT','Private')
Tapplied = 'PRIVATE'
return [text, M_text, Tapplied]
But I'm not sure how to append this to the existing dataframe.
Can anyone please suggest how to do this in python?