亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

循環數據框的每一行,并根據條件向數據框添加元素

循環數據框的每一行,并根據條件向數據框添加元素

慕碼人8056858 2023-09-26 16:45:12
我想循環數據框的每一行,如果列和列表中的字符串之間存在匹配,我會在新列中添加一個元素。在此示例中,我想添加一個新列來對產品進行分類。因此,如果該列的一行與其中一個列表匹配,則類別可以是“飲料”或“食品”,如果沒有匹配,則類別將為其他。list_drinks={'Water','Juice','Tea'}list_food={'Apple','Orange'}data = {'Price':  ['1', '5','3'], 'Product': ['Juice','book', Pen]}for (i,j) in itertools.zip_longest(list_drinks,list_food):    for index in data.index:         if(j in data.loc[index,'product']):            data["Category"] = "Food"        elif(i in data.loc[index,'product']):            data["Category"] ="drinks"        else:            data["Category"]="Other"           輸出將是:Price  Product Category 1      Juice    drinks 5      book     Other 3      Pen      Other我的問題主要是我不知道如何匹配列表和行之間的模式。我也嘗試過: str.contains但沒有成功。
查看完整描述

2 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

無需循環。您可以使用.isin()withnp.select()根據條件返回結果。見下面的代碼:


import pandas as pd

import numpy as np

list_drinks=['Water','Juice','Tea']

list_food=['Apple','Orange']

data = {'Price':  ['1', '5','3'],

    'Product': ['Juice','book','Pen']}

df = pd.DataFrame(data)

df['Category'] = np.select([(df['Product'].isin(list_drinks)),

               (df['Product'].isin(list_food))],

              ['drinks',

              'food'], 'Other')

df

Out[1]: 

  Price Product Category

0     1   Juice   drinks

1     5    book    Other

2     3     Pen    Other

下面,我將代碼分解為更詳細的內容,以便您可以了解它是如何工作的。我也根據你的評論略有改變。我使用列表理解和 來檢查列表中的值是否位于數據幀中的值的子字符串中in。為了提高匹配率,我還將 as 全部小寫與 進行比較.lower():


import pandas as pd

import numpy as np

list_drinks=['Water','Juice','Tea']

list_food=['Apple','Orange']

data = {'Price':  ['1', '5','3'],

    'Product': ['green Juice','book','oRange you gonna say banana']}

df = pd.DataFrame(data)

c1 = (df['Product'].apply(lambda x: len([y for y in list_drinks if y.lower() in x.lower()]) > 0))

c2 = (df['Product'].apply(lambda x: len([y for y in list_food if y.lower() in x.lower()]) > 0))

r1 = 'drinks'

r2 = 'food'


conditions = [c1,c2]

results= [r1,r2]


df['Category'] = np.select(conditions, results, 'Other')

df

Out[1]: 

  Price                      Product Category

0     1                  green Juice   drinks

1     5                         book    Other

2     3  oRange you gonna say banana     food



查看完整回答
反對 回復 2023-09-26
?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

這是一個替代方案 -


import itertools

import pandas as pd


list_drinks={'Water','Juice','Tea'}

list_food={'Apple','Orange'}

data = pd.DataFrame({'Price':  ['1', '5','3'], 'Product': ['Juice','book', 'Pen']})

category = list()

for prod in data['Product']: 

    if prod in list_food:

        category.append("Food")

    elif prod in list_drinks:

        category.append("drinks")

    else:

        category.append("Other")

data['Category']= category

print(data)

輸出-


Price  Product Category

 1      Juice    drinks

 5      book     Other

 3      Pen      Other


查看完整回答
反對 回復 2023-09-26
  • 2 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號