简体   繁体   中英

From one string column creates two columns, one using the first three elements, and the other using .format()

I've searched many questions here and I couldn't find a proper answer to me, so pls help me

A string column in df

Farms
Albatros
Bali
Casablanca

Desired output

Farms ACR sourcekey
Albatros Alb Db_Alb_key
Bali Bal Db_Bal_key
Casablanca Cas Db_Cas_key

My main focus here is to have a unique source key, because after, I need to create those tables on the Database.

So what is the best solution, thinking in performance. Should I do a foreach? Should I create the ACR(acronym) table?

I am using python version 3.8.10

If you need any more information, please let me know. I am just a noob and sometimes is really frustrating when we get stuck.

Thank you so much!

Simply use slicing and vectorial string addition:

df['ACR'] = df['Farms'].str[:3]
df['sourcekey'] = 'Db_' + df['ACR'] + '_key'

output:

        Farms  ACR   sourcekey
0    Albatros  Alb  Db_Alb_key
1        Bali  Bal  Db_Bal_key
2  Casablanca  Cas  Db_Cas_key

This should work for you:


df['ACR'] = df.Farms.apply(lambda x: x[:3])
df['sourcekey'] = df.ACR.apply(lambda x: 'Db_'+x+'key')

Output:

>>df
    Farms      ACR      sourcekey
0   Albatros    Alb     Db_Albkey
1   Bali        Bal     Db_Balkey
2   Casablanca  Cas     Db_Caskey

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM