gpt4 book ai didi

python - Pandas 比较两个字符串列以创建第三列

转载 作者:行者123 更新时间:2023-12-02 18:02:20 32 4
gpt4 key购买 nike

我的数据框包含两列不同的类钻石、黄金和白银

class_pd = pd.DataFrame({'old_class':['gold', 'gold' , 'silver'],
'new_class':['diamond', 'silver', 'silver']})

我想创建一个新列,显示类已升级降级

我尝试过的

我编写了以下函数来设置规则

def status_desc(class_pd, old_class, new_class):
if ((class_pd['old_class'] == 'gold') & (class_pd['new_class'] == 'diamond') or \
(class_pd['old_class'] == 'silver') & (class_pd['new_class'] == 'diamond') or \
(class_pd['old_class'] == 'silver') & (class_pd['new_class'] == 'gold')):
val = 'Upgrade'
elif ((class_pd['old_class'] == 'diamond') & (class_pd['new_class'] == 'gold') or \
(class_pd['old_class'] == 'diamond') & (class_pd['new_class'] == 'silver') or \
(class_pd['old_class'] == 'gold') & (class_pd['new_class'] == 'silver')):
val = 'Downgrade'
else:
val = 'NA'

然后我尝试使用以下方法将该函数应用于我的数据框

class_pd['class_desc'] = class_pd.apply(lambda x: status_desc(class_pd['old_class'], class_pd['new_class']), axis=1)

错误

我收到此错误

TypeError: status_desc() missing 1 required positional argument: new_class

所需输出

class_pd = pd.DataFrame({'old_class':['gold', 'gold' , 'silver'],
'new_class':['diamond', 'silver', 'silver'],
'class_desc':['Upgrade','Downgrade', 'NA']})

最佳答案

另一个解决方案 pd.Categorical ,对我来说似乎更优雅并且更具可扩展性:

categories = ['silver', 'gold', 'diamond']
class_pd = class_pd.apply(pd.Categorical, categories=categories, ordered=True)

class_pd['class_desc'] = 'NA'

class_pd.loc[class_pd.old_class > class_pd.new_class, 'class_desc'] = 'Downgrade'
class_pd.loc[class_pd.old_class < class_pd.new_class, 'class_desc'] = 'Upgrade'

我们告诉 Pandas 固有的顺序,然后可以使用比较运算符。

@jezrael 建议使用 numpy.select 完成最后一点(添加类别后)的另一种方法:

import numpy as np

conditions = [
class_pd.old_class < class_pd.new_class,
class_pd.old_class > class_pd.new_class,
class_pd.old_class == class_pd.new_class,
]
labels = ["Upgrade", "Downgrade", "NA"]
class_pd["class_desc"] = np.select(conditions, labels)

关于python - Pandas 比较两个字符串列以创建第三列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74054640/

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