我在stackoverflow中找不到它,所以我想问这个问题。你知道吗
假设我有两列:数据帧中的A,B,它只由一堆单词组成,我想创建一个新的列C,它基于以下规则为TRUE/FALSE:
If word in B = word in A + 'ing', then it's True or vice versa
If word in B = word in A + 'ment', then it's True of vice versa.
所以我定义了以下函数:
def parts_of_speech(s1, s2):
return s1+'ing'==s2 or s1+'ment'==s2 or s1+s1[-1]+'ing'==s2
例如
A B C
Engage Engagement True
Go Going True
Axe Axis False
Management Manage True
我尝试了以下方法:
df['C']=df.apply(lambda x: parts_of_speech(x.A, x.B) or
parts_of_speech(x.B, x.A) )
或者
df['C']=df.apply(parts_of_speech(df['A'], df['B']) or
parts_of_speech(df['A'], df['B']) )
我得到同样的错误:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
我不知道我做错了什么。有什么简单的解决办法吗?你知道吗
任何帮助都将不胜感激。你知道吗
。默认情况下,“应用”可用于列。示例中唯一需要的更改是添加
axis=1
以应用于行:对于您的示例数据:
或者使用你的方法,但矢量化:
输出:
相关问题 更多 >
编程相关推荐