0

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?

Sai
  • 47
  • 1
  • 2
  • 7
  • You can iterate through rows and append to a new dataframe – Akash Feb 23 '21 at 18:28
  • But I want the old data as well. I just want to enter a new row – Sai Feb 23 '21 at 18:32
  • Your question is a little confusing. You switch between wanting to add a new row and a new column, which your output shows. It would help if you [edit] to include your sample input and output data as text in your question, not as a picture or link, to make a [mcve]. See [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – G. Anderson Feb 23 '21 at 18:34
  • Basically start with an empty dataframe, now iterare rows in your original dataframe, insert one row into new dataframe, now perform the condition and update the row, now update the new row into the 2nd dataframe. – Akash Feb 24 '21 at 16:55

0 Answers0