[SOLVED] create pandas column with function based on multiple columns and multiple string constants


I would like to create a func that gets several pandas columns and make extra string manipulation to create another column. The string maipulatioins has to be dependent on some other string constants that are sent to the function.

import pandas as pd
items_df = pd.DataFrame({
    'id': [302, 504, 708, 103, 343, 565],
    'name': ['Watch', 'Camera', 'Phone', 'Shoes', 'Laptop', 'Bed'],
    'surname': ['McArthur', 'Martinez', 'Whatever', 'Oslo', 'Berlin', 'Furniture'],
    'price': [300, 400, 350, 100, 1000, 400],
    'discount': [10, 15, 5, 0, 2, 7]

# column depending on two columns (strings) and other with f strings
def create_new(idd,name,header,footer):
    # this does not work
        result = header + 'web' + idd + name.str + footer
        print('result does not work')
        result = f'''{header}.web{idd}{name}{footer}'''
        print('result2 does not work')
    result = result + "?serveAsMime=application/pdf;"
    return result

header = 'https//:'
footer = '.com'
items_df['link'] = create_new(items_df['id'].apply(str),items_df['name'],header,footer)

In the function the result 1 does not work (error) and the result 2 works but does not calculate the result as desired but adds the whole series in each element.

So basically the question is how do I pass to a function two series and two strings and make strings operations with them.
The expected output of items_df[‘link’] are strings like ‘https//:web302Watch.com’ (for row one)

what I actually get is:
https//:.web0 302\n1 504\n2 708\n3 … (for row one)



items_df['url'] = 'https://web'+items_df['id'].astype(str)+items_df['name']+'.com'


    id    name    surname  price  discount                       url
0  302   Watch   McArthur    300        10   https://web302Watch.com
1  504  Camera   Martinez    400        15  https://web504Camera.com
2  708   Phone   Whatever    350         5   https://web708Phone.com
3  103   Shoes       Oslo    100         0   https://web103Shoes.com
4  343  Laptop     Berlin   1000         2  https://web343Laptop.com
5  565     Bed  Furniture    400         7     https://web565Bed.com

Answered By – Scott Boston

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published.