简体   繁体   English

从一个字符串列创建两列,一列使用前三个元素,另一列使用.format()

[英]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 df 中的字符串列

Farms农场
Albatros信天翁
Bali巴厘岛
Casablanca卡萨布兰卡

Desired output所需 output

Farms农场 ACR ACR sourcekey源密钥
Albatros信天翁 Alb阿尔布 Db_Alb_key Db_Alb_key
Bali巴厘岛 Bal巴尔 Db_Bal_key Db_Bal_key
Casablanca卡萨布兰卡 Cas卡斯 Db_Cas_key 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.

相关问题 如何使用其他两列(熊猫)中的数据替换一列中的字符串 - How do I replace a string from one column using the data from two other columns (pandas) 使用两列连接,在四个其他数据帧中填充一个pandas数据帧中的列 - using two column join, populate columns in one pandas dataframe from four other dataframes 需要帮助使用python pandas模块从减去其他两列中创建一列 - Need help using python pandas module to create one column from subtracting other two columns 按一列分组数据,并从另外两列中选择第一个出现的数据 - Group data by one column and select first occurences from two other columns 使用数据框中的两列创建一个字典,一列中有重复项 - Create a dict using two columns from dataframe with duplicates in one column 将数据从一列分为三列 - Separate data from one column into three columns 从其他两列替换一列中的 NaN 值 - Replace NaN values in one column from two other columns 使用基于其他两列的条件时显示一列的值 - Display values of one column when using conditionals based on two other columns 根据 pandas dataframe 中的其他三列更改一列的值 - Changing values of one column based on the other three columns in pandas dataframe Python:如何基于从一侧到另一侧的两列合并两个数据框? - Python: how to merge two dataframes based on two columns from one side and one column to the other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM