简体   繁体   English

从列值中拆分并获取字符串的一部分,并从pandas python中创建新列

[英]Split and take part of string from column values and make new column from that in pandas python

I have a strings like this as a value of one column in my df. 我有一个这样的字符串作为我的df中一列的值。

ttt-OPP/MKKL-7/LNFFF-74/OOOP-71/AAD-1/RRR-232
ttt-OPP/MKKL-7/LNFFF-89/OOOP-71/AAD-1/RRR-232

How to get new column with part of this columns. 如何使用此列的一部分获取新列。 Part that I need is 我需要的部分是

74
89

string.split() allows you to explode a string into a list of parts according to a separator (here / and - ). string.split()允许您根据分隔符(此处为/- )将字符串分解为部件列表。

s = 'ttt-OPP/MKKL-7/LNFFF-74/OOOP-71/AAD-1/RRR-232'
print s.split('/')[2].split('-')[1]
# 74

Use pandas.apply() to apply it to your column 使用pandas.apply()将其应用于您的列

df['b'] = df['a'].apply(lambda s:s.split('/')[2].split('-')[1])
print (df)

output 产量

                                              a   b
0  ttt-OPP/MKKL-7/LNFFF-74/OOOP-71/AAD-1/RRR-232  74
1  ttt-OPP/MKKL-7/LNFFF-89/OOOP-71/AAD-1/RRR-232  89

nb: Use @A-Za-z 's solution, it's faster than mine. nb:使用@ A-Za-z的解决方案,它比我的快。

If this is the df 如果这是df

    val
0   ttt-OPP/MKKL-7/LNFFF-74/OOOP-71/AAD-1/RRR-232
1   ttt-OPP/MKKL-7/LNFFF-89/OOOP-71/AAD-1/RRR-232

You can use str.extract 您可以使用str.extract

df['num_val'] = df.val.str.extract('LNFFF-(\d+)/', expand = False)

You get 你得到

    val                                             num_val
0   ttt-OPP/MKKL-7/LNFFF-74/OOOP-71/AAD-1/RRR-232   74
1   ttt-OPP/MKKL-7/LNFFF-89/OOOP-71/AAD-1/RRR-232   89

假设您的数据框名为df且列col:

 df['sub_col'] = pd.Series([s[21:23] for s in df['col'].values], index=df.index)

It seems you need str.extract : 看来你需要str.extract

df = pd.DataFrame({'a': ['ttt-OPP/MKKL-7/LNFFF-74/OOOP-71/AAD-1/RRR-232',
                         'ttt-OPP/MKKL-7/LNFFF-89/OOOP-71/AAD-1/RRR-232']})  
print (df)
                                               a
0  ttt-OPP/MKKL-7/LNFFF-74/OOOP-71/AAD-1/RRR-232
1  ttt-OPP/MKKL-7/LNFFF-89/OOOP-71/AAD-1/RRR-232

df['new'] = df['a'].str.extract('LNFFF-(\d+)', expand=False)
#if necessary convert to ints
df['new'] = df['new'].astype(int)
print (df)
                                               a new
0  ttt-OPP/MKKL-7/LNFFF-74/OOOP-71/AAD-1/RRR-232  74
1  ttt-OPP/MKKL-7/LNFFF-89/OOOP-71/AAD-1/RRR-232  89

Solution with splitting by split and selecting by indexing with str : 通过split并通过索引使用str进行选择的解决方案:

df['new'] = df['a'].str.split('/').str[2].str.extract('(\d+)', expand=False)
print (df)
                                               a new
0  ttt-OPP/MKKL-7/LNFFF-74/OOOP-71/AAD-1/RRR-232  74
1  ttt-OPP/MKKL-7/LNFFF-89/OOOP-71/AAD-1/RRR-232  89

df['new'] = df['a'].str.split('/').str[2].str.split('-').str[1]
print (df)
                                               a new
0  ttt-OPP/MKKL-7/LNFFF-74/OOOP-71/AAD-1/RRR-232  74
1  ttt-OPP/MKKL-7/LNFFF-89/OOOP-71/AAD-1/RRR-232  89

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Openpyxl:如何从列中获取值,append 将其转换为字符串,然后使新字符串成为数组的一部分? - Python Openpyxl: How to take values from a column, append it to a string and then make the new string part of an array? 如何从 Python Pandas 中的 DataFrame 中的列中获取字符串值? - How to take part of string value from column in DataFrame in Python Pandas? python pandas,从现有列创建一个新列,从字符串中取出x个字符 - python pandas , create a new column from an existing column , take the x n umber of characters from a string 从一列pandas python中的字符串切片创建新列 - Make new column from slice of string from one column pandas python 熊猫从另一列的字符串切片创建新列 - Pandas make new column from string slice of another column Pandas - 使用另一列部分的平均值创建新列 - Pandas - make new column with mean values of the part of another column 如果有值,则从另一列中获取新列,如果没有,则从另一列中获取值 Python Pandas - New column from another column if value, if not, take value from another column Python Pandas 如何在 Pandas 列中提取部分字符串并创建一个新列 - How to extract part of a string in Pandas column and make a new column 熊猫-更快的方式来分割最后\\和在新列中使用字符串的一部分 - Pandas - Faster way to split by last \ and use part of string in new column 根据来自其他列 pandas python 的特定值创建一列 - Make a column based on specific values from other column pandas python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM