[英]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我在这里搜索了很多问题,但找不到合适的答案,所以请帮助我
Farms![]() |
---|
Albatros![]() |
Bali![]() |
Casablanca![]() |
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?
我应该做一个foreach吗? Should I create the ACR(acronym) table?
我应该创建 ACR(首字母缩写词)表吗?
I am using python version 3.8.10我正在使用 python 版本 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: 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: Output:
>>df
Farms ACR sourcekey
0 Albatros Alb Db_Albkey
1 Bali Bal Db_Balkey
2 Casablanca Cas Db_Caskey
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.