Home > Software engineering >  How to move text from old column to newly created columns pandas
How to move text from old column to newly created columns pandas

Time:10-14

I have the following code which is applied just for one column named 'Desc'. How can i change the code to apply it for two columns? 'Desc' and 'Desc1'?

df = pd.DataFrame({'Desc':['cat is black','dog is white']})
kw = ['cat','dog']
for k in kw:
   df[k   ' col'] = df.Desc.map(lambda s: s if k in s else '' )

[The output with just one column 'Desc' is:]

1

But now i want to use this function to do the selection of two colums Desc and Desc1

CodePudding user response:

You didn't show what result you expect but you can always use

df[k   ' col'] = df.Desc.map(...)   ","   df.Desc1.map(...)

But this will add , in empty cell and it will repeate duplicated values.

import pandas as pd

df = pd.DataFrame({
    'Desc':  ['cat is black', 'dog is white'],
    'Desc1': ['cat is white', 'dog is white'],
})

kw = ['cat','dog']
for k in kw:
   df[k   ' col'] = df.Desc.map(lambda s: s if k in s else '')   ','   df.Desc1.map(lambda s: s if k in s else '')
   
print(df.to_string())

Result:

           Desc         Desc1                    cat col                    dog col
0  cat is black  cat is white  cat is black,cat is white                          ,
1  dog is white  dog is white                          ,  dog is white,dog is white

But you can also use .apply(function, args=[...], axis=1) to send full row to function and run more complex code in function

import pandas as pd

df = pd.DataFrame({
    'Desc':  ['cat is black', 'dog is white'],
    'Desc1': ['cat is white', 'dog is white'],
})

def select(row, word):
    result = []

    if word in row['Desc']:
        result.append(row['Desc'])

    if word in row['Desc1']:
        result.append(row['Desc1'])
        
    # skip duplicated
    if len(result) > 1 and result[0] == result[1]:
        result = result[:1]
        
    return ",".join(result)
    
kw = ['cat','dog']
for word in kw:
   df[f'{word} col'] = df.apply(select, args=[word], axis=1)
   
print(df.to_string())

Result:

           Desc         Desc1                    cat col       dog col
0  cat is black  cat is white  cat is black,cat is white              
1  dog is white  dog is white                             dog is white

CodePudding user response:

import pandas as pd


df = pd.DataFrame({'Desc':['cat is black','dog is white']})
kw = ['cat','dog']

i = 0 #iterator
for k in kw:
    #Creates an empty column
    df[k   'col'] = "" 
    #Assigns the value in the cell based on its location in df["Desc"]
    df[k   'col'][i] = df["Desc"][i]
    i = i 1 #iterates
  • Related