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
try:
result = header 'web' idd name.str footer
except:
print('result does not work')
try:
result = f'''{header}.web{idd}{name}{footer}'''
except:
print('result2 does not work')
print(type(result))
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)
CodePudding user response:
IIUC:
items_df['url'] = 'https://web' items_df['id'].astype(str) items_df['name'] '.com'
print(items_df)
Output:
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