[英]Assign value if a specific words precedes different word in a new column
Add a new column when the below conditions are achieved.满足以下条件时添加新列。
My code is as below:我的代码如下:
for i in range(len(df)):
if df['Type'][i] == "Action" < df['Type'][i] == "Scene":
i.append(1)
I'm going to assume you're using pandas.我假设您使用的是 pandas。 Normally you don't want to iterate over each row like the way you're doing.
通常你不想像你正在做的那样迭代每一行。 What you can do is create an intermediary column which is just all the values shifted up by 1
您可以做的是创建一个中间列,它只是将所有值上移 1
eg df['shifted_Type'] = df['Type'].shift(-1)例如 df['shifted_Type'] = df['Type'].shift(-1)
import pandas as pd
def foo(row):
if row['type'] == 'action' and row['shifted_type'] == 'scene':
return 1
else: # add other comparisons here
return 0
df = pd.DataFrame()
# setting up a sample df
df['type'] = ['scene', 'action', 'action', 'scene','action']
# creating new column of 'type' shifted backward by 1
df['shifted_type'] = df['type'].shift(-1)
# creating new column of result with the specified logic in foo applied
df['result'] = df.apply(foo, axis=2)
print(df)
OUTPUT: OUTPUT:
type shifted_type result
0 scene action 0
1 action action 0
2 action scene 1
3 scene action 0
4 action NaN 0
@ajoseps Thanks for your great effort. @ajoseps 感谢您的努力。
Yes, I am using pandas but I don't want to shift type.是的,我正在使用 pandas 但我不想改变类型。
I am classifying my scenes into 3 categories: So if the word "Action" comes before "Scene" this type is labeled as 0 in a new column.我将我的场景分为 3 类:因此,如果“动作”一词出现在“场景”之前,则此类型在新列中标记为 0。 And if the word "Dialogue" comes before "Scene" this type is labeled as 1 in the same new column.
如果“对话”一词出现在“场景”之前,则此类型在同一新列中标记为 1。
Thanks, @ajoseps your above code is working I made small amendments only as below:谢谢,@ajoseps 你上面的代码正在工作我只做了如下的小修改:
def foo(df):
if df['Type'] == 'Action' and df['shifted_type'] == 'Scene':
return 1
elif df['Type'] == 'Dialogue' and df['shifted_type'] == 'Scene':
return 2
elif df['Type'] == 'Scene' and df['shifted_type'] == 'Scene':
return 3
else:
return 0
df['shifted_type'] = df['Type'].shift(-1)
df['result'] = df.apply(foo, axis=1)
df['result'] = df.apply(foo, axis=1)
pd.options.display.max_rows = None
df
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.