【发布时间】:2017-09-05 22:23:58
【问题描述】:
我有一个大约 50 列的 df:
Product ID | Cat1 | Cat2 |Cat3 | ... other columns ...
8937456 0 5 10
8497534 25 3 0
8754392 4 15 7
Cat 表示该产品有多少数量属于某个类别。现在我想添加一个“类别”列,表示产品的主要类别(忽略其他列,只考虑 Cat 列)。
df_goal:
Product ID | Cat1 | Cat2 |Cat3 | Category | ... other columns ...
8937456 0 5 10 3
8497534 25 3 0 1
8754392 4 15 7 2
我想我需要使用 max 和 apply 还是 map?
我在 stackoverflow 上找到了这些,但它们没有解决类别分配问题。在 Excel 中,我将列从 Cat 1 重命名为 1 并使用 index(match(max))。
Python Pandas max value of selected columns
How should I take the max of 2 columns in a dataframe and make it another column?
【问题讨论】:
标签: python pandas numpy max apply