gpt4 book ai didi

python - 数据框根据该类别中的字符串值列表将列更新为类别名称

转载 作者:行者123 更新时间:2023-11-28 21:32:18 24 4
gpt4 key购买 nike

我有按名称分类的列表,例如:

dining = ['CARLS', 'SUBWAY', 'PIZZA']
bank = ['TRANSFER', 'VENMO', 'SAVE AS YOU GO']

如果在其他列中找到任何这些字符串,我想将新列更新为类别名称。我的其他问题的一个例子here ,我有以下数据集(银行交易列表示例):

import pandas as pd
import numpy as np

dining = ['CARLS', 'SUBWAY', 'PIZZA']
bank = ['TRANSFER', 'VENMO', 'SAVE AS YOU GO']

data = [
[-68.23 , 'PAYPAL TRANSFER'],
[-12.46, 'RALPHS #0079'],
[-8.51, 'SAVE AS YOU GO'],
[25.34, 'VENMO CASHOUT'],
[-2.23 , 'PAYPAL TRANSFER'],
[-64.29 , 'PAYPAL TRANSFER'],
[-7.06, 'SUBWAY'],
[-7.03, 'CARLS JR'],
[-2.35, 'SHELL OIL'],
[-35.23, 'CHEVRON GAS']
]

df = pd.DataFrame(data, columns=['amount', 'details'])
df['category'] = np.nan
df

amount details category
0 -68.23 PAYPAL TRANSFER NaN
1 -12.46 RALPHS #0079 NaN
2 -8.51 SAVE AS YOU GO NaN
3 25.34 VENMO CASHOUT NaN
4 -2.23 PAYPAL TRANSFER NaN
5 -64.29 PAYPAL TRANSFER NaN
6 -7.06 SUBWAY NaN
7 -7.03 CARLS JR NaN
8 -2.35 SHELL OIL NaN
9 -35.23 CHEVRON GAS NaN

是否有一种有效的方法可以根据是否在 data.details 中找到列表中的字符串来将类别列更新为“餐饮”或“银行”?

I.e. Desired Output:
amount details category
0 -68.23 PAYPAL TRANSFER bank
1 -12.46 RALPHS #0079 NaN
2 -8.51 SAVE AS YOU GO bank
3 25.34 VENMO CASHOUT bank
4 -2.23 PAYPAL TRANSFER bank
5 -64.29 PAYPAL TRANSFER bank
6 -7.06 SUBWAY dining
7 -7.03 CARLS JR dining
8 -2.35 SHELL OIL NaN
9 -35.23 CHEVRON GAS NaN

根据我之前的问题,到目前为止,我假设我需要使用使用 str.extract 创建的新列表。

最佳答案

我们可以用 np.select 来做到这一点因为我们有多个条件:

dining = '|'.join(dining)
bank = '|'.join(bank)

conditions = [
df['details'].str.contains(f'({dining})'),
df['details'].str.contains(f'({bank})')
]

choices = ['dining', 'bank']

df['category'] = np.select(conditions, choices, default=np.NaN)

   amount          details category
0 -68.23 PAYPAL TRANSFER bank
1 -12.46 RALPHS #0079 nan
2 -8.51 SAVE AS YOU GO bank
3 25.34 VENMO CASHOUT bank
4 -2.23 PAYPAL TRANSFER bank
5 -64.29 PAYPAL TRANSFER bank
6 -7.06 SUBWAY dining
7 -7.03 CARLS JR dining
8 -2.35 SHELL OIL nan
9 -35.23 CHEVRON GAS nan

关于python - 数据框根据该类别中的字符串值列表将列更新为类别名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56841186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com