[英]How to convert the values in the row with same id to one list? (Pandas in python)
I uploaded the csv file我上传了csv文件
#Open the first dataset
train=pd.read_csv("order_products__train.csv",index_col="order_id")
The data looks like:数据看起来像:
product_id
order_id
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
2 5
2 6
What I want is the data frame looks like,我想要的是数据框的样子,
order_id product_id
1 1,2,3,4
2 1,2,3,4,5,6
Since I want to generate a list like因为我想生成一个列表
[[1,2,3,4],[1,2,3,4,5,6]]
Could anyone help?有人能帮忙吗?
You can use the the function .groupby()
to do that您可以使用 function
.groupby()
来做到这一点
train = train.groupby(['order_id'])['product_id'].apply(list)
That would give you expected output:这会给你预期的 output:
order_id
1 [1, 2, 3, 4]
2 [1, 2, 3, 4, 5]
Finally, you can cast this to a DataFrame or directly to a list to get what you want:最后,您可以将其转换为 DataFrame 或直接转换为列表以获得您想要的内容:
train = train.to_frame() # To pd.DataFrame
# Or
train = train.to_list() # To nested lists [[1,2,3,4],[1,2,3,4,5]]
There must be better ways but I guess you can simply do the following:必须有更好的方法,但我想你可以简单地执行以下操作:
list_product = []
for i in train["order_id"].unique():
tmp = train[train["order_id"] == i]
list_product.append(tmp["product_id"].to_list())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.