This question already has an answer here:
I am a complete newbie to Python (and Pandas library) and need to recreate some SQL code in it.
My task is quite simple of the face of it, I have a few columns, and i need to search them for specific strings, and if they exist then a value is placed in category columns.
I have listed an example below, however that is for illustrative purposes, in reality the list of words I will need to search will be quite large and it was suggested creating separate list with search words would be a better way to go about it.
e.g.
import pandas as pd
phone_ds= [('3UKIPHONE_CONTRACT', 968), ('IPHONESIMONLYO2123456', 155), ('3UKPAYGCDDDNEXUS', 77), , ('VODAF1234GOOGLCONTRACT', 973)]
a = pd.DataFrame(data=phone_ds, columns=['Names', 'qty'])
def f(a):
if a['Names'].str.contains('3UK'):
company = 'Three'
if a['Names'].str.contains('IPHONE'):
OS = 'iOS'
.
.
.
etc
Is there a better (more efficient) way than listing if statements?
How would i go about adding the results into new columns? e.g extra columns for Company, brand, Carrier etc etc
Thanks!
Aucun commentaire:
Enregistrer un commentaire