我的dataframe具有值:
data_df
0 student
1 sample text
2 student
3 no students
4 sample texting
5 random sample
我使用正则表达式用"学生"提取行,我的结果如下:
regexdf
0 student
2 student
我的目标是在主数据框架中创建一个新的列,并具有0和1值。即第0行应该为1,第5行应该为零。(作为" RegexDf"第0和2行中的"学生")如何匹配两者中的索引并创建列?
使用REGEX:
data_df = data_df.assign(regexdf = data_df[1].str.extract(r'(student)b', expand=False))
data_df['student'] = data_df['regexdf'].notnull().mul(1)
print(data_df)
输出:
1 regexdf student
0 student student 1
1 sample text NaN 0
2 student student 1
3 no students NaN 0
4 sample texting NaN 0
5 random sample NaN 0
编辑
df_out = data_df.join(regexdf, rsuffix='regex')
df_out['pattern'] = df_out['1regex'].notnull().mul(1)
df_out['Count_Pattern'] = df_out['pattern'].cumsum()
print(df_out)
输出:
1 1regex pattern Count_Pattern
0 student student 1 1
1 sample text NaN 0 1
2 student student 1 2
3 no students NaN 0 2
4 sample texting NaN 0 2
5 random sample NaN 0 2
您也可以做
df['bool'] = df[1].eq('student').astype(int)
或
df['bool'] = df[1].str.match(r'(student)b').astype(int)
1 bool
0 student 1
1 sample text 0
2 student 1
3 no students 0
4 sample texting 0
5 random sample 0
如果您想要一个新的数据框,则
ndf = df[df[1].eq('student')].copy()