[英]Python Pandas populate column from partial string match
I have a dataframe like below, and I need to create a new column Block
with either the value 1
or 2
in it based on a partial string match in the column Program Number
where it says _block_1 or _block_2. 我有一个如下所示的数据
Block
,我需要根据“ Program Number
”列中表示“ _block_1”或“ _block_2”的部分字符串匹配,在其中创建一个新列“ Block
,其值为1
或2
。 I've been trying if statements and .str.contains
but can't get it to work. 我一直在尝试如果语句和
.str.contains
但不能使其正常工作。 How would you do this? 你会怎么做?
148 0209-3SP_block_1 ['g76p010060q00250r.0005' 'JEBD0507160 REV A' CHNCIII
149 0209-3SP_block_2 ['g76x.3761z-.500p03067q03067f.05' 'JEBD0507160 REV A' CHNC III
150 0209-5SP_block_1 ['g76p020060q00250r.0005' 'JEBD0507160 REV A' CHNC III
151 0209-5SP_block_2 ['g76x.3767z-.48p03067q03067f.05' 'JEBD0507160 REV A' CHNC III
152 0210-3SP_block_1 ['g76p010060q00250r.0005' 'JEBD0507160 REV A' CHNC III
You could use the method where
from numpy
: 您可以使用
numpy
where
方法:
import numpy as np
df['Block'] = np.where(
df['Machine'].str.contains('_block_1'),1,
np.where(df['Machine'].str.contains('_block_2'),2,0)
)
Otherwise, assuming all the strings have the same length: 否则,假设所有字符串具有相同的长度:
df['Block'] = df['Machine'].str[15:].astype(int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.