[英]Is there a way in pandas to create an integer in a new column if a row contains a specific string
跑:
df['Number'] = df.svn_changes.str.match(r'r\d+').cumsum()
Yes, is contains
with regex and cumsum
:是的,
contains
正则表达式和cumsum
:
df = pd.DataFrame({'svn_changes':['r123456','RowValueRow','ValueRowValue',
'some_string_string','r234566','ValueRowValue',
'some_string_string','r123789','something_here',
'ValueRowValue','String_2','String_4']})
df['Number'] = df['svn_changes'].str.contains('r\d+').cumsum()
print(df)
Output:输出:
svn_changes Number
0 r123456 1
1 RowValueRow 1
2 ValueRowValue 1
3 some_string_string 1
4 r234566 2
5 ValueRowValue 2
6 some_string_string 2
7 r123789 3
8 something_here 3
9 ValueRowValue 3
10 String_2 3
11 String_4 3
Here's a simple reusable line you can use to do that:这是一个简单的可重复使用的行,您可以使用它来做到这一点:
df['new_col'] = df['old_col'].str.contains('string_to_match')*1
The new column will have value 1
if the string is present in this column, and 0
otherwise.如果该列中存在字符串,则新列的值为
1
,否则为0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.