简体   繁体   English

如果特定单词在新列中位于不同单词之前,则赋值

[英]Assign value if a specific words precedes different word in a new column

Add a new column when the below conditions are achieved.满足以下条件时添加新列。

  1. if "Action" comes before "Scence" ==> Assign value equal 1 (in a new column)如果“动作”出现在“场景”之前 ==> 赋值等于 1(在新列中)
  2. if "Dialogue" comes before "Scence" ==> Assign value equal 2 (in a new column)如果“对话”出现在“场景”之前 ==> 赋值等于 2(在新列中)
  3. if "Scence" comes before "Scence" ==> Assign value equal 3 (in a new column)如果“Scence”出现在“Scence”之前 ==> 赋值等于 3(在新列中)

在此处输入图像描述

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.

相关问题 创建新列以指定列是否包含单词 - Creating new columns to assign value whether a column contains a word 如何将索引 1 处特定列的值分配给新变量 - How do I assign to a new variable the value of a specific column at index 1 如何将不同行的值分配给新列 - How to assign different row's value to new column 如何将不同行的排名值分配给新列? - How to assign different row‘s rank value to new column? 使用正则表达式在不同列的熊猫数据框中查找单词并创建新值 - Find words and create new value in different column pandas dataframe with regex 如何将特定列中的单词作为 label 分配给新的 dataframe - How can I assign the words from a specific column as a label to a new dataframe 如何根据2个列表检查多个单词是否在dataframe值的字符串中,然后将值分配给新列 - How to check if multiple words are in a string of a dataframe value based on 2 lists, then assign a value to a new column 如何计算单词出现次数(从特定列表中的单词)并将结果存储在 Python 中的 Pandas Dataframe 中的新列中? - How to count the word occurence (from words in specific list) and store the results in a new column in a Pandas Dataframe in Python? 基于不同列中特定值的计算的新列 - New column based on calculations on a specific value in a different column 如何通过特定列分配新的值列? - how to assign new column of values by a specific column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM