简体   繁体   English

python pandas如果列字符串包含单词标志

[英]python pandas if column string contains word flag

I want to add a flag to my python pandas dataframe df, if an entry in the column Title contains the word test (upper or lower case or all Uppercase), I would like to add T in a new column test . 我想在我的python pandas dataframe df中添加一个标志,如果Title列中的条目包含单词test (大写或小写或全部大写),我想在新的列test添加T

This gives me an error and does not account for all uppercase scenarios: 这给我一个错误,并不能解决所有大写情况:

df['Test_Flag'] = np.where(df[df['Title'].str.contains("test|Test")==True], 'T', '')

ValueError: Length of values does not match length of index

You need contains with parameter case=False and na=False : 您需要contains参数case=Falsena=False

df['Test_Flag'] = np.where(df['Title'].str.contains("test", case=False, na=False), 'T', '')

Sample: 样品:

df = pd.DataFrame({'Title':['test','Test',np.nan, 'a']})
df['Test_Flag'] = np.where(df['Title'].str.contains("test", case=False, na=False), 'T', '')
print (df)
  Title Test_Flag
0  test         T
1  Test         T
2   NaN          
3     a  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Pandas 使用正则表达式从包含字符串的列中提取单词 - Python Pandas Extract word from column that contains String with Regex 如果列表中的列中的单词在新列中添加Y标志,则为python熊猫 - python Pandas if word in column in list add Y flag in new column Python pandas:如果第 2 列不包含“字符串”,则从第 1 列获取唯一 ID - Python pandas: get unique id from column 1 if "string" not contains on column 2 Python Pandas 如果另一列包含某个字符串,如何更新列 - Python Pandas how to update a column if another column contains a certain string Pandas DataFrame-检查A列中的字符串是否包含B列中的完整单词字符串 - Pandas DataFrame - check if string in column A contains full word string in column B 使用字符串包含标记字符串中的确切单词 - Flag Exact Word Within a String Using String Contains 如果一列的字符串包含 pandas dataframe 中另一列的单词,如何删除整行 - How to drop entire row if string of one column contains the word from another column in pandas dataframe Python 搜索词包含:在一个字符串中 - Python Search word contains : in a string 基于python pandas中的标志拆分列值 - Split column values based on flag in python pandas 根据列是否包含给定字符串设置标志列 - Setting flag column depending on whether column contains a given string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM