我正在尝试在pandas数据框中基于dataframe中已经存在的另一列SearchCol3
构建一个新列NewCol4
。测试SearchCol3
的每个值,看它是否包含列表stings
中的任何子字符串。如果SearchCol3
中的值包含列表strings
中的一个子字符串,则列表replacement
中的相应值将插入到子字符串所在行的列NewCol4
中。如果在SearchCol3
的值中找不到子字符串,Col2
中的值将插入NewCol4
。在
预期结果:
Col1 Col2 SearchCol3 NewCol4
0 20 'May' 'abc(feb)' 'February'
1 30 'March' 'def | mar' 'March'
2 40 'June' 'ghi | feb' 'February'
3 50 'July' 'jkl(apr)' 'April'
4 60 'May' 'mno(mar)' 'March'
5 70 'March' 'abc' 'March'
目前我正在使用此代码的工作。在
^{pr2}$我的数据、搜索和替换数据帧和列表比本例要长得多。我正在寻找比我目前使用的更有效的方法来节省时间。有什么建议吗?在
还有一种方法:
这对我很有用,而且从好的方面来说,是非常可读的!在
输出:
^{pr2}$一。str.提取物接受正则表达式。在
http://pandas.pydata.org/pandas-docs/version/0.15.2/generated/pandas.core.strings.StringMethods.extract.html#pandas.core.strings.StringMethods.extract
输出:
^{pr2}$相关问题 更多 >
编程相关推荐