如何根据数字前某个数字和其余数字剪切字符串
这是我的数据
Id actual_pattern
1 100101
2 10101
3 1010101
4 101
这是预期的输出
对于 cut_pattern1
是 actual_pattern
中的前 4 位数字对于 cut_pattern2
是来自 cut_pattern1
的其余形式,如果来自 cut_pattern1
的其余形式不存在,则使 cut_pattern2
= 0
如果 cut_pattern2
中的任何1
,则使 binary_cut2
= 1 否则使 binary_cut2
= 0
Id actual_pattern cut_pattern1 cut_pattern2 binary_cut2
1 100101 1001 01 1
2 10101 1010 1 1
3 1010101 1010 101 1
4 101 101 0 0
通过使用 str
、 replace
用于更改空字符串和新列来创建新列 使用Series.str.contains
转换为整数:
df['actual_pattern'] = df['actual_pattern'].astype(str)
df['cut_pattern1'] = df['actual_pattern'].str[:4]
df['cut_pattern2'] = df['actual_pattern'].str[4:].replace('','0')
df['binary_cut2'] = df['cut_pattern2'].str.contains('1').astype(int)
print (df)
Id actual_pattern cut_pattern1 cut_pattern2 binary_cut2
0 1 100101 1001 01 1
1 2 10101 1010 1 1
2 3 1010101 1010 101 1
3 4 101 101 0 0
编辑:
@Rick希区柯克评论的解决方案:
df['actual_pattern'] = df['actual_pattern'].astype(str)
df['cut_pattern1'] = df['actual_pattern'].str[:4]
df['cut_pattern2'] = df['actual_pattern'].str[4:].replace('','0')
df['binary_cut2'] = df['cut_pattern2'].str.contains('1').astype(int)
print (df)
Id actual_pattern cut_pattern1 cut_pattern2 binary_cut2
0 1 100101 1001 01 1
1 2 10101 1010 1 1
2 3 1010101 1010 101 1
3 4 00001111 0000 1111 1
以下是我如何处理这个问题:
s = df.actual_pattern.astype(str).str
# Split into 2 lists, the first containing the first 4 digits
out = s.split(r'(d{4})').str[-2:].values.tolist()
# [['1001', '01'], ['1010', '1'], ['1010', '101'], ['101']]
# build a dataframe from the lists
out = pd.DataFrame(out, columns=['cut_pattern1', 'cut_pattern2'])
# fill missing values (absense of string in list) with 0
out['cut_pattern2'] = out.cut_pattern2.fillna('0')
out['binary_cut2'] = out.cut_pattern2.str.contains('1').view('i1')
print(out)
cut_pattern1 cut_pattern2 binary_cut2
0 1001 01 1
1 1010 1 1
2 1010 101 1
3 101 0 0
在这里使用一些正则表达式和字符串提取:
m=df.actual_pattern.str.extract('(?P<cut_pattern1>.{,4})(?P<cut_pattern2>.*)').replace('',0)
cut_pattern1 cut_pattern2
0 1001 01
1 1010 1
2 1010 101
3 101 0
然后做:
m.assign(binary_cut2=m.cut_pattern2.str.contains('1',na=False).astype(int))
cut_pattern1 cut_pattern2 binary_cut2
0 1001 01 1
1 1010 1 1
2 1010 101 1
3 101 0 0
最后将其连接到原始 df:
m=df.actual_pattern.str.extract('(?P<cut_pattern1>.{,4})(?P<cut_pattern2>.*)').replace('',0)
m=m.assign(binary_cut2=m.cut_pattern2.str.contains('1',na=False).astype(int))
pd.concat([df,m],axis=1)
Id actual_pattern cut_pattern1 cut_pattern2 binary_cut2
0 1 100101 1001 01 1
1 2 10101 1010 1 1
2 3 1010101 1010 101 1
3 4 101 101 0 0