2017-07-03 74 views

回答

4

string.split()允許您根據隔板(這裏/-)爆炸字符串轉換部分的列表。

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

使用pandas.apply()將它應用到你的專欄

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

輸出

           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 

注:使用@A-Za-z的解決方案,它比我更快。

+0

尼斯之一。問題:如果我還需要應用一個條件會怎麼樣?即:如果's'包含字符串'OPP',則只填寫'df ['b']' –

1

假設你的數據框被稱爲DF和你列col:

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

如果是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 

您可以使用str.extract

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

你得到

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 
1

看來你需要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 

解決方案通過split並選擇由indexing with 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 
相關問題