查找 ia strn 列在同一數據框中的列表列中,並創建具有值的第三列 (Find ia a strn column is in a list column in the same data frame and create a 3rd column with a value)


問題描述

查找 ia strn 列在同一數據框中的列表列中,並創建具有值的第三列 (Find ia a strn column is in a list column in the same data frame and create a 3rd column with a value)

我有這個包含 2 列的數據框,“Column A” 和“B列”,A列是一個字符串,B列是一個列表:

A        B                               c
cat      | cat | elephant | gorilla |    YES
dog      | monkey | duck | giraffe |     NO
bird     | cow | bird | hamster |        YES

我想評估A列是否在B列中如果是這樣在這個新列 C 中寫“是”或“否”

我嘗試了很多方法,最後一個是:

df_epl["Marketo LSC"] = df_epl["Data Entry Point"].isin("Entry Point List")

但它給了我這個錯誤:

in isin

raise TypeError(TypeError: only list‑like objects are allowed to be passed to isin(), you passed a [str]


參考解法

方法 1:

Try this

import pandas as pd
df = pd.read_csv('res.csv') #Your csv here
C = []
for i in range(0,len(df)):
  if df['A'][i] in df['B'][i]:
    C.append('YES')
  else:
    C.append('NO')
df['C'] = C
print(df)

(by BernySURYA TEJA)

參考文件

  1. Find ia a strn column is in a list column in the same data frame and create a 3rd column with a value (CC BY‑SA 2.5/3.0/4.0)

#string #Python #operator-keyword #list #comparison






相關問題

VB.net 如何讓流閱讀器忽略某些行? (VB.net how to make stream reader ignore some line?)

Perl Text::CSV_XS 從字符串中讀取 (Perl Text::CSV_XS read from string)

在 D3 中用逗號格式化數字 (Formatting numbers with commas in D3)

我應該使用什麼-String 或 StringBuilder 將 SQL 查詢存儲在使用許多不同 SQL 查詢的代碼中 (what should i use-String or StringBuilder for storing SQL queries in a code which uses many different SQL queries)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

使用正則表達式處理字符串 (String Manipulation with regular expression)

如何區分數字字符串和字符串? (How do i distinguish between number string and character string?)

如何將單詞的結尾與 Ruby 中的哈希進行比較? (How can I compare the ending of a word with a hash in Ruby?)

在 Python 列表中查找位於特定字符串之間的字符串 (Find Strings Located Between Specific Strings in List Python)

大寫替代字母 (Alternate letters in upper case)

查找 ia strn 列在同一數據框中的列表列中,並創建具有值的第三列 (Find ia a strn column is in a list column in the same data frame and create a 3rd column with a value)

在算術表達式中使用字符串是否安全? (Is it safe to use strings in arithmetic expressions?)







留言討論