python pandas根据其他列中的条件进行新列分类

吉安杰

使用以下python pandas dataframe df:

df = pd.DataFrame({'transaction_id': ['A123','A123','B345','B345','C567','C567','D678','D678'], 
                   'product_id': [255472, 251235, 253764,257344,221577,209809,223551,290678],
                   'product_category': ['X','X','Y','Y','X','Y','Y','X']})

transaction_id | product_id | product_category
A123              255472             X
A123              251235             X
B345              253764             Y
B345              257344             Y
C567              221577             X
C567              209809             Y
D678              223551             Y
D678              290678             X

我需要添加另一列“ transaction_category”,以查看transaction_id和transaction_id中的产品类别。这是我正在寻找的输出:

transaction_id | product_id | product_category | transaction_id
123              255472             X                X only
123              251235             X                X only
345              253764             Y                Y only
345              257344             Y                Y only
567              221577             X                X & Y
567              209809             Y                X & Y
678              223551             Y                X & Y
678              290678             X                X & Y

请注意,我的数据框中还有其他未使用的列,所以我想我需要从grouby开始吗?

df2 = df.groupby(['transaction_id','product_category']).reset_index()
BEN_YO

IIUC通过使用transformjoin

df.groupby('transaction_id').product_category.transform(lambda x : '&'.join(set(x)))
Out[468]: 
0      X
1      X
2      Y
3      Y
4    X&Y
5    X&Y
6    X&Y
7    X&Y
Name: product_category, dtype: object

从斯科特比赛中,您的预期成绩是:

df['transaction_category']=df.groupby('transaction_id')['product_category'].transform(lambda x: x + ' only' if len(set(x)) < 2 else ' & '.join(set(x)))
df
Out[479]: 
  product_category  product_id transaction_id transaction_category
0                X      255472           A123               X only
1                X      251235           A123               X only
2                Y      253764           B345               Y only
3                Y      257344           B345               Y only
4                X      221577           C567                X & Y
5                Y      209809           C567                X & Y
6                Y      223551           D678                X & Y
7                X      290678           D678                X & Y

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

根据Python Pandas中的其他列对列进行分组

Python-根据其他列的条件创建新列

Python:根据其他两列中的值有条件地创建新列

Pandas/Python:如何根据其他列的值创建新列并将额外条件应用于此新列

根据其他列中的值在python 3(pandas)数据框中创建新列

根据python pandas中其他列的值创建新列

根据python pandas数据框中其他列的值计算新列

Python pandas - groupby之后,如何根据其他列中的值创建新列

Python Pandas:根据其他空白列的条件使用填充

如何根据python中其他列的条件计算值?

Python Pandas:根据其他列中的唯一标识符创建具有最小值的新列

根据其他多个列的条件创建新的Python DataFrame列

使用其他两列中的数组创建新列,并在python pandas中对其进行测试

Pandas/Python - 根据其他列的交叉引用创建新列

Python Pandas:对列中的值进行分类并创建一个新列

如何根据python中其他列的多个条件更新数据框中的现有列?

python dataframe根据其他列的条件替换列中的部分字符串

如何根据python中其他列的单元格条件移动列的位置

Python groupby-根据其他列中的条件更改列值

根据 Python 中其他列的条件绘制数据框的列

Python groupby - 根据其他列中的值创建一个新列

使用python中的pandas根据其他列中给出的值选择列

Pandas / Python:根据行值和其他DataFrame设置新列的值

Python pandas 根据它们的值和其他行对多列进行排序

如何通过使用python中的现有列创建以其他列为条件的新列

根据其他列中的最大值填充列(python pandas)

Python - Pandas - 根据其他列的值替换列中的字符串

根据其他列值/ Pandas -Python 在数据框中创建 ID 列

如何基于Python Pandas中的其他列在DataFrame中创建新列?